[유니티 플랫폼 별 경로들 모음]

더보기

[윈도우 에디터]
Application.persistentDataPath : 사용자디렉토리/AppData/LocalLow/회사이름/프로덕트이름
파일 읽기 쓰기 가능
Application.dataPath : 프로젝트디렉토리/Assets
Application.streamingAssetsPath : 프로젝트디렉토리/Assets/StreamingAssets
파일 읽기 쓰기 가능


[윈도우 응용프로그램]
Application.persistentDataPath : 사용자디렉토리/AppData/LocalLow/회사이름/프로덕트이름
파일 읽기 쓰기 가능
Application.dataPath : 실행파일/실행파일_Data
Application.streamingAssetsPath : 실행파일/실행파일_Data/StreamingAssets
파일 읽기 쓰기 가능

[맥 에디터]
Application.persistentDataPath : 사용자디렉토리/Library/Caches/unity.회사이름.프로덕트이름
파일 읽기 쓰기 가능
Application.dataPath : 프로젝트디렉토리/Assets
Application.streamingAssetsPath : 프로젝트디렉토리/Assets/StreamingAssets
파일 읽기 쓰기 가능

[맥 응용프로그램]
Application.persistentDataPath : 사용자디렉토리/Library/Caches/unity.회사이름.프로덕트이름
파일 읽기 쓰기 가능
Application.dataPath : 실행파일.app/Contents
Application.streamingAssetsPath : 실행파일.app/Contents/Data/StreamingAssets
파일 읽기 쓰기 가능

[웹 플랫폼]
웹에서는 명시적인 파일 쓰기 불가능. 애셋번들로 해야함
Application.persistentDataPath : /
Application.dataPath : unity3d파일이 있는 폴더
Application.streamingAssetsPath : 값 없음.

[안드로이드 External]
Application.persistentDataPath : /mnt/sdcard/Android/data/번들이름/files
파일 읽기 쓰기 가능
Application.dataPath : /data/app/번들이름-번호.apk
Application.streamingAssetsPath : jar:file:///data/app/번들이름.apk!/assets 
파일이 아닌 WWW로 읽기 가능/쓰기 불가능

[안드로이드 Internal] 
Application.persistentDataPath : /data/data/번들이름/files/
파일 읽기 쓰기 가능
Application.dataPath : /data/app/번들이름-번호.apk
Application.streamingAssetsPath : jar:file:///data/app/번들이름.apk!/assets
파일이 아닌 WWW로 읽기 가능/쓰기 불가능

[iOS]
Application.persistentDataPath : /var/mobile/Applications/프로그램ID/Documents 
파일 읽기 쓰기 가능
Application.dataPath : /var/mobile/Applications/프로그램ID/앱이름.app/Data
Application.streamingAssetsPath : /var/mobile/Applications/프로그램ID/앱이름.app/Data/Raw 
파일 읽기 가능/쓰기 불가능

출처: https://qits.tistory.com/entry/유니티-플랫폼별-경로-정리 [Quiet, In The Storm...]

 

위의 자료에서 보듯이 플랫폼 별로 파일의 경로가 다른 것을 볼 수 있는데, 이러한

 

경로의 다양성으로 인해서 각각의 플랫폼별에 맞게 코드 상에서 작업을 해줘야한다.

 

물론, 유니티에서는 기본적으로 Application.~path로 어느정도 정돈이 된 class를 받아올 수 있다.

 

쓰고나서 읽는 형태로 빌드를 해서 올린 상태에서 쓰고 읽는 형태라면 읽기 쓰기 가능한 경로인

 

Application.persistantDataPath를 활용하면 별 어려움 없이 사용이 가능하다. 하지만, 내가 필요했던 내용은

 

에디터 상에서 맵 관련 파일들을 생성한 뒤, 그 파일들을 이용하고자 했기에 이미 저장되어있는 파일들을

 

사용할 방법이 필요했다.

 

현재는 안드로이드와 PC에서 에디터 상에서만 작업을 하고 있지만, 추후에는 iOS쪽도 추가할 예정이므로

 

나중에 추가를 한다고 생각하고 코드를 작성했다.

T LoadJsonFile<T>(string loadPath, string fileName, RuntimePlatform platform)
{
        string jsonData = "";
        if (platform == RuntimePlatform.WindowsEditor)
        {
            FileStream fileStream = new FileStream(string.Format("{0}/{1}.json", loadPath, fileName), FileMode.Open);
            byte[] data = new byte[fileStream.Length];
            fileStream.Read(data, 0, data.Length);
            fileStream.Close();
            jsonData = Encoding.UTF8.GetString(data);
        }
        else
        {
#if ANDROID_GOOGLEGAMES
            var filePath = loadPath + fileName + ".json";
            UnityWebRequest www = UnityWebRequest.Get(filePath);
            www.SendWebRequest();
            while (!www.isDone) { } //완료될 때까지 대기

            byte[] resultBytes = www.downloadHandler.data;
            jsonData = Encoding.UTF8.GetString(resultBytes);
#endif
        }
        return JsonUtility.FromJson<T>(jsonData);
}

현재 작성 중인 코드에서 json 파일을 이용하는데, json 파일은 범용적으로 사용되는 형태의 파일이므로 path를 잡을 때,

 

윈도우 에디터 즉, 유니티 상에서 사용하는 경우도 있기에 현재 실행 중인 플랫폼이 무엇인지 받아오는 매개변수를 받아

 

처리를 함으로써, 모바일 환경과 PC에서 에디터에서 실제 작업하는 환경에서도 동작하도록 구현하였다.

 

그리고 UnityWebRequest 클래스를 사용하였는데, 이를 사용하기 위해선 UnityEngine.Networking을 넣어줘야한다.

 

기본적으로 WWW클래스를 사용하여서도 작업은 가능하지만, 유니티에서 추천을 하지 않는 방식이라고 하므로,

 

UnityWebRequest로 변경하였다. 동작의 형태는 WWW와 UnityWebRequest 둘이 같다고 봐도 무방하다. 

 

UnityWebRequest가 좀 더 세분화되고 잘 구현이 되어있다고 보는게 맞을 것 같다.

string GetPathByPlatform(RuntimePlatform platform)
{
        string filePath = "";
        if (platform == RuntimePlatform.WindowsEditor)
        {
            filePath = Path.Combine(Application.dataPath + "/StreamingAssets/추가경로/");
            return filePath;
        }
        else
        {
#if ANDROID_GOOGLEGAMES
            filePath = Application.streamingAssetsPath + "/추가경로/";
#endif 
        }
        return filePath;
}

위의 코드는 Json에서 사용했다고 작성한 코드에서 플랫폼별로 다른 경로를 사용해야하기에 #if/#endif를

 

사용하여 구분을 하였다. 추후에 iOS도 작업을 해야한다. 추가경로의 부분에 경로를 넣게되면,

 

Android의 경우 StreamingAssets라는 폴더가 통째로 복사되어서 넘어가는 형태이기에 폴더를 구분지어

 

사용할 수 있다. 그냥 StreamingAssets라는 폴더에 바로 넣고 사용한다면, 추가경로 부분에 "/"를 추가하여서,

 

작성을 하면 될 것이다.


WebRequest가 무엇인지 공부를 해봐야할 것 같다.

 

WebRequest는 인터넷에서 데이터에 액세스하기 위한 NET Framework의 요청/응답 모델에 대한

추상(Visual Basic의 MustInherit) 기본 클래스입니다. 

 

라고 간단한 정의를 찾아볼 수는 있었는데 아직 웹 쪽으로는 모르는 부분이 많아서 무슨소린지 정확히는 모르겠다.

 

www.isDone이라는 bool값이 있는 것을 보면 비동기로 동작을 하기에 데이터를 받아오기까지 기다린 뒤

 

받은 데이터를 가지고 처리를 하는 것 같기는 하다.

 

작성한 글의 오타나 코드 상의 버그나 좀 더 개선될 수 있는 부분이 있다면 지적해주신다면 감사히받겠습니다.

+ Recent posts