Game Engine/Unity

Unity Addressable과 같은 Bundle 시스템을 이용하여 Video Data를 받아 재생하고 싶을 때

OneSeat 2022. 4. 22.
반응형

🧱 사양

Windows 10, Unity 2021 LTS

💣 문제

아래에서 설명할 방법은 일종의 편법(?)입니다.

동영상을 편하게 재생하려면 해당 프로젝트에 "StreamingAssets"이라는 폴더를 만들어 동영상을 넣고 URL을 연결해주면 됩니다.

https://docs.unity3d.com/ScriptReference/Video.VideoPlayer.html

 

Unity - Scripting API: VideoPlayer

Content can be either a VideoClip imported asset or a URL such as file:// or http://. Video content will be projected onto one of the supported targets, such as camera background or RenderTexture. If the video content includes transparency, this transparen

docs.unity3d.com

 

그러나 빌드를 했을 때 앱 용량을 줄이기 위해 번들 시스템을 이용하여 다양한 리소스들을 로드하기도 합니다. VideoClip 또한 마찬가지로 로드를 하려고 했지만 계획대로 되지 않았습니다.

 

 

💡 해결

저는 결과적으로 Addressable을 사용하지 않았습니다.

그러나 다른 방법이 없고 Addressable 같은 Bundle 시스템을 무조건 사용해야 하며 잘 안될 때 사용할 방법입니다.

 

일단 동영상 포맷이 사용 가능한 건지부터 확인합니다.

https://docs.unity3d.com/Manual/VideoSources-FileCompatibility.html

 

Unity - Manual: Video file compatibility

Understanding video files Video file compatibility You can import many video file formats into Unity. Unity stores imported video files as VideoClip assets. For Unity to preview video files, the files must be compatible with the platform where you run the

docs.unity3d.com

 

포맷도 맞는 것 같다면 아래는 편법(?)을 사용한 방법입니다.

 

정보를 찾아보니 ".bytes" 확장자를 이용한 방법이었습니다.

1. 본인의 동영상 파일의 확장자를 .bytes로 변경합니다.

2. Asset Load 시에 TextAsset으로 Load 합니다.

3. Application.persistentDataPath에 원하는 경로 저장하는데 저장할 때 확장자를 ".mp4"로 저장합니다.

4. VideoPlayer에 해당 경로 URL을 저장하고 진행합니다.

 

private IEnumerator LoadVideo(string pathVideo)
    {
        textAssetBundle = DownloadingManager.Instance.LoadAssetAsync<TextAsset>(pathVideo);
 
        yield return textAssetBundle;
 
        TextAsset textAsset = (TextAsset)textAssetBundle.asset;
 
        File.WriteAllBytes(Path.Combine(Application.persistentDataPath, pathVideo + ".mp4"), textAsset.bytes);
 
        string url = Application.persistentDataPath + "/" + pathVideo + ".mp4";
 
        videoPlayer.url = url;
        ...
    }

PC와 Android에서 정상적으로 재생이 되는 것을 확인하였습니다.

iOS는 확인하기 전에 다른 방법으로 진행하기로 하여 확인이 중단되었습니다.

 

참고한 원문 링크입니다.

https://forum.unity.com/threads/load-videoclip-in-assetbundle-on-android.466850/page-2

 

Video - Load VideoClip in assetbundle on Android

Hi, I have some problem to get my assetloader work on Android. I try to load VideoClip from assetBundle. It works great on editor but when i try this...

forum.unity.com

 

💫 단점

메모리 로드가 된 것을 직접 사용하면 좋겠지만 그렇게 하지 않고 파일을 만들어 저장까지 하면 메모리를 2중으로 사용하게 됩니다. 

파일을 저장한 뒤에 메모리 로드가 된 것을 release 하면 되긴 하지만 그전까지는 순간적으로 메모리를 더 사용하게 됩니다.

 
반응형

댓글