유니티 Split 사용법

상세 컨텐츠

본문 제목

유니티 Split 사용법

유니티

by 수군 2020. 4. 22. 22:17

본문

유니티를 하다보면은 에쎗폴더 내용을 캔버스에 노출 시키거나 별도의 표현법을 사용해야 되는 경우가 있다. 

enum DialogKey

{

 

}

 

public class DialogsManager : MonoBehaviour
{
    // 1. 파일 로드한다.
    // key => value
    Dictionary<string, string> dialogTable = new Dictionary<string, string>();

    // 싱글톤
    public static DialogsManager Instance;

    private void Awake()
    {
        if(Instance == null)
        {
            Instance = this;
        }
        LoadDialog();
    }
    // 1. 파일 로드해서 dialogTable 에 값을 넣어준다.
    void LoadDialog()
    {
        //폴더에 있는 내용 불러오기 
        string url = Application.dataPath + "/Dialogs/Resources/NPC01/";
        print("dialog url : " + url);
        string[] path = Directory.GetFiles(url);
        print("path : " + path.Length);
        foreach(string link in path)
        {
            //뒤에 있는 확장자명 빼기 
            if(link.Substring(link.Length - 3, 3) == "txt")
            {
                string[] fileName = link.Split('/');
                print("fileName : " + fileName[fileName.Length-1]);
                string f = fileName[fileName.Length - 1];
                string Key = f.Substring(0, f.Length - 4);
                print("key ====> " + Key);
                string fileText = File.ReadAllText(link);

                dialogTable.Add(Key, fileText);
            }
        }
        //string path = "";
        //TextAsset file = (TextAsset)Resources.Load("NormalDialog_Eng_01");
        //dialogTable.Add("NormalDialog_Eng_01", file.text);
    }

    public string GetDialog(string key)
    {
        return dialogTable[key];
    }

 

싱글톤으로 사용한 폴더 부르기 방법이기도 하고 Split 사용법이다.

많은 글을 찾아봤지만 이래저래 도움 받아서 작성한 코드 

 

 

 

쿠팡파트너스 활동으로 일부 수익이 발생합니다. 

반응형

관련글 더보기