Unity download a video from server and save it

亡梦爱人 提交于 2019-12-11 04:38:20

问题


I want to fetch a video from my server and save it in my assests to view it later in my game. I am aware of use of www. but i dont understand how to download the video from my server giving it's url. below is the code to get video as a texture.

    var www = new WWW("http://Sameer.com/SampleVideo_360x240_2mb.mp4");
    var movieTexture = www.movie;

Any idea how do I save the mp4 file ?


回答1:


Using UnityWebRequest API.

You can use UnityWebRequest

    public class VideoDownloader: MonoBehaviour {
        void Start() {
            StartCoroutine(DownloadVideo());
        }

        IEnumerator DownloadVideo() {
            UnityWebRequest www = UnityWebRequest.Get("https://example.com/video.mp4");
            yield return www.SendWebRequest();

            if(www.isNetworkError || www.isHttpError) {
                Debug.Log(www.error);
            } else {
                File.WriteAllBytes("path/to/file", www.downloadHandler.data);
            }
        }
    }

Using obsolete WWW API.

You can use WWW.bytes to get the raw data for movie file, and save that. Something like:

    var www = new WWW("http://Sameer.com/SampleVideo_360x240_2mb.mp4");
    File.WriteAllBytes("path/to/file", www.bytes);


来源:https://stackoverflow.com/questions/33872545/unity-download-a-video-from-server-and-save-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!