using System.Collections;
using System.IO;
using UnityEngine;
public class DownVideo : MonoBehaviour
{
FileStream fs;
string path = null;
public VideoPlayer video;
void Awake()
{
StartCoroutine(DownOrLoadVideo("视频名字", "http://kanyikan.oss-cn-shanghai.aliyuncs.com/kanrongmei/%E6%B1%89%E5%85%B3%E7%B4%AB%E7%A0%82.mp4"));
}
IEnumerator DownOrLoadVideo(string downloadVideoName, string videoURL)
{
#if UNITY_EDITOR
path = Application.dataPath + @"/Video";
#elif UNITY_IPHONE || UNITY_ANDROID
string[] src = new string[1] { "Android" };
string[] srcs = Application.persistentDataPath.Split(src, System.StringSplitOptions.None);
path = srcs[0] + @"/Video";
#endif
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
Debug.Log("下载" + path);
if (File.Exists(path + @"/" + downloadVideoName + ".mp4"))
{
path += "/" + downloadVideoName + ".mp4";
video.url = path ;
}
else
{
WWW www = new WWW(videoURL);
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log("请求失败");
}
else
{
fs = File.Create(path + "/" + downloadVideoName + ".mp4");
fs.Write(www.bytes, 0, www.bytes.Length);
fs.Close();
path += "/" + downloadVideoName + ".mp4";
video.url = path ;
Debug.Log("视频下载成功");
}
}
}
}
|