소소한 개발 공부
[Unity] 파일 입출력 how to open binary file in unity mobile app 본문
빌드한 파일 안에 있는 파일을 읽으려고 하는데 모바일과 윈도우 에디터 환경이 달라 빌드 환경에서 파일을 찾지 못하는 일이 있었다. 아래의 사이트에서 해결 방안을 얻어서 기록을 남긴다.
https://answers.unity.com/questions/974942/accessing-binary-file-on-android-via-www.html
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
또한 using 문을 썼기 때문에 close를 해주지 않아도 자동으로 객체가 dispose 된다.
'개발 > Unity' 카테고리의 다른 글
ML-agent 2.2.1 설치 (0) | 2022.11.22 |
---|---|
[Unity] TextMeshPro Dilate script로 제어하기 how to edit TextMeshPro Dilate (0) | 2022.10.05 |
[C#] 초(sec)를 시간 형식으로 바꾸기 (0) | 2022.08.18 |
FPS 캐릭터 이동 CharacterController (0) | 2022.03.31 |
List 에 배열 넣기, List에 원소들 한 번에 넣기 (0) | 2022.03.23 |