본문 바로가기

Unity & C#

[모바일] 파일 읽기 쓰기

string path;

if UNITY_EDITOR
	path = string.Format(@"Assets/StreamingAssets/{0}", FILE_NAME);
else
	path = string.Format("{0}/{1}", Application.persistentDataPath, FILE_NAME);

//읽고 쓰기할 수 있는 해당 위치에 파일이 존재하지 않는다면
if (!File.Exists(path))
{
#if UNITY_ANDROID
    WWW loadDb = new WWW("jar:file://" + Application.dataPath + "!/assets/" + FILE_NAME);  // 스트리밍 에셋 AOS 경로
    while (!loadDb.isDone) { }
    File.WriteAllBytes(path, loadDb.bytes); //스트리밍에 있는 파일을 읽고 쓸수 있는 경로에 복사
#elif UNITY_IOS
    string loadDb = Application.dataPath + "/Raw/" + FILE_NAME; // 스트리밍 에셋 iOS 경로
    File.Copy(loadDb, path);
#else
    string loadDb = Application.dataPath + "/StreamingAssets/" + FILE_NAME;  
    File.Copy(loadDb, path);
#endif
}

해당 코드를 이용하여 읽고 쓸수 있는 AOS, iOS 경로에 파일을 복사하여 수정하거나 이용할 수 있도록 한다.

 

'Unity & C#' 카테고리의 다른 글

[모바일] Unity SQLite DB 사용  (0) 2023.03.24