[Unity] 파일 입출력 how to open binary file in unity mobile app
빌드한 파일 안에 있는 파일을 읽으려고 하는데 모바일과 윈도우 에디터 환경이 달라 빌드 환경에서 파일을 찾지 못하는 일이 있었다. 아래의 사이트에서 해결 방안을 얻어서 기록을 남긴다.
https://answers.unity.com/questions/974942/accessing-binary-file-on-android-via-www.html
Accessing binary file on Android via WWW - Unity Answers
answers.unity.com
public void Process()
{
StartCoroutine(LoadFileOnAndroid(fileName));
}
IEnumerator LoadFileOnAndroid(string fileName)
{
string path = "jar:file://" + Application.dataPath + "!/assets/" + fileName;
using (WWW file = new WWW(path))
{
yield return file;
MemoryStream ms = new MemoryStream(file.bytes);
BinaryReader reader = new BinaryReader(ms);
// reader 로 파일 내용 활용
}
}
나는 파일을 StreamingAssets 폴더 안에 넣었고, StreamingAssets 폴더의 모바일 상 경로는 아래와 같아서
string path = "jar:file://" + Application.dataPath + "!/assets/" + fileName; 로 작성했다.
jar:file:///data/app/번들이름.apk!/assets
* 파일이 아닌 WWW로 읽기 가능
출처: https://3dmpengines.tistory.com/1745
유니티 데이터 패스(Application.dataPath, ) 정리 와 예약 폴더들(Resources, )
유니티 데이터 패스 정리 테스트한 기기에 따라 결과가 다를 수 있음 [윈도우 에디터] Application.persistentDataPath : 사용자디렉토리/AppData/LocalLow/회사이름/프로덕트이름 파일 읽기 쓰기 가능 Applicatio
3dmpengines.tistory.com
또한 using 문을 썼기 때문에 close를 해주지 않아도 자동으로 객체가 dispose 된다.
[유니티 C# 강좌] 12. 네임스페이스(Namespaces), using 문
프로그래밍을 할 때, 가장 큰 어려움은 겪는 것이 클래스 이름, 함수 이름, 메서드 이름, 변수 이름을 정하는 것입니다. 그리고 프로젝트의 규모가 크면 커질수록 각종 이름들의 충돌 발생 가능
coderzero.tistory.com