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 ?
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);