Unity download a video from server and save it

2019-07-17 02:38发布

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条回答
叼着烟拽天下
2楼-- · 2019-07-17 03:09

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);
查看更多
登录 后发表回答