티스토리 뷰

반응형

[unity/android] 유니티에서 안드로이드 외부저장소 정리(persistentDataPath/streaming Asset)

 

유니티 환경에서 외부 파일을 저장/불러오기 작업을 진행 하게되었는데, 생각보다 PC와는 환경이 달라서 공유합니다

 

[안드로이드 저장소]

Application.persistentDataPath : /Android/data/com.YourProductName.YourCompanyName/files [파일 읽기/ 쓰기 가능]

Application.dataPath : /data/app/번들이름-번호.apk

Application.streamingAssetsPath : jar:file:///data/app/번들이름.apk!/assets [파일이 아닌 WWW로 읽기 가능]

안드로이드에서 스트리밍 어셋은 읽기전용에 WWW를 사용해서 가져와야하기때문에 비추천

통상적으로 Application.persistentDataPath를 이용하여 저장/불러오기를 진행한다

 

[윈도우 어플리케이션 / 에디터]

Application.persistentDataPath : 사용자디렉토리/AppData/LocalLow/회사이름/프로덕트이름 파일 읽기 쓰기 가능

Application.dataPath : 프로젝트디렉토리/Assets

Application.streamingAssetsPath : 프로젝트디렉토리/Assets/StreamingAssets 파일 읽기 쓰기 가능

윈도우에서는 스트리밍어셋을 이용하지만, 안드로이드와 함께 진행하는 프로젝트는 역시 ` Application.persistentDataPath`로 진행하는게 좋고, 경로는 조금 아쉬운감은 있지만 잘 기능한다 ㅎㅎ

 

#구현 코드

[읽기]

    public void ReadText()
    {
        var txtFile = Application.persistentDataPath + "/coordinate.json";
    
        FileStream filestream = new FileStream(txtFile, FileMode.Open, FileAccess.Read);
        StreamReader sr = new StreamReader(filestream, System.Text.Encoding.UTF8);
        var parsingText = sr.ReadToEnd();
        sr.Close();
        filestream.Close();

        text.text = parsingText;
    }

 

[쓰기]

    public void WriteFile(string json)
    {
        var filePath = Application.persistentDataPath+"/coordinate.json";
        File.WriteAllText(filePath, json);
    }

 

#persistentDataPath #안드로이드 #저장소 #android path #내부저장소 #유니티 #unity

반응형
댓글
반응형