ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Unity] 유니티 StreamingAssets 폴더 활용
    Unity 2020. 4. 20. 20:58

    유니티에서 리소스들은 Assets 폴더에 위치한다. 그런데 이 곳에 위치한 모든 리소스가 최종 완성된 바이너리에 포함되지는 않는다. 최종 바이너리에 포함되는 리소스는 Assets/Resources에 들어있는 것들이다. 이 리소스들의 경우는 Resources.Load()를 사용해 쉽게 로드할 수 있다.

     

    이외에도 스트리밍에 필요한 멀티미디어 파일들은 StreamingAssets 폴더에 넣을 수 있다. 이 폴더에 위치한 리소스들은 타겟 머신에서 유니티가 지정한 위치로 이동되기에 플랫폼에 상관 없이 리소스에 접근하기 좋은 위치로 추천된다. 이 패스는 Application.streamingAssetsPath를 사용해서 액세스 할 수있다.

     

    Assets/StreamingAssets/sample.mp3이 있다고 가정하자. 런타임에 sample.mp3를 재생하기 위해서는 다음과 같이 할 수 있다.

     

    	// Assets/StreamingAssets/sample.mp3를 가리키는 패스 설정
    #if UNITY_EDITOR
    	// 유니티 에디터일 경우
        string urlAudio = "file://" + Application.streamingAssetsPath + "/sample.mp3";
    #else
    	// 런타임일 경우
    	string urlAudio = Application.streamingAssetsPath + "/sample.mp3";
    #endif
    
    	// gameObject에 붙은 AudioSoure 생성
    	AudioSource audioSrc = gameObject.AddComponent<AudioSource>();
    
    	// sample.mp3를 입력으로 설정
    	audioSrc.clip = urlAudio;
    
    	// volume 설정
    	audioSrc.volume = 0.5f;
    
    	// 재생
    	audioSrc.Play();

     

    이와같이 StreamingAssets 폴더를 활용하면 플랫폼에 독립적으로 접근할 수 있는 코드를 만들 수 있다. 실제 이 패스의 위치는 타겟 머신에 따라 달라지는데, 다음은 몇몇의 예시이다.

     

    Platform Actual path to StreamingAsssets
    macOS Application.dataPath + "/Resources/Data/StreamingAssets"
    iOS Application.dataPath + "/Raw"
    Android (compressed APK/JAR 내부의 파일을 이용) "jar:file://" + Application.dataPath + "!/assets"

    Most Others (Windows, Linux players, PS4, Xbox One, Switch)

    Application.dataPath + "/StreamingAssets"

     

     

    Fin.

    반응형

    댓글

Calvin's Memo