using System.Collections; using System.Collections.Generic; using System.IO; using System.Net; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; public class GetVideoImage : MonoBehaviour { public string url; RawImage rawImage; MeshRenderer render; AVProVideoPlayer ap; string localpath; private void OnEnable() { if (localpath != "" && localpath != null) { if (File.Exists(localpath)) { this.GetComponent().SetUrl(localpath); this.GetComponent().Play(); } } } private void Start() { localpath = Application.persistentDataPath + "/OOBE/" + url.Split('/')[url.Split('/').Length-1]; ap = this.GetComponent(); rawImage = this.GetComponent(); render = this.GetComponent(); if(!File.Exists(localpath)) { if(!Directory.Exists(Application.persistentDataPath + "/OOBE")) Directory.CreateDirectory(Application.persistentDataPath + "/OOBE"); OnDownloadAssets(); } } private void OnDownloadAssets()//下载资源 { StartCoroutine(DownloadFormServer_IE(url)); } //其他方法 private IEnumerator DownloadFormServer_IE(string url)//从服务器下载资源 { Debug.Log("正在下载" + url); UnityWebRequest request = UnityWebRequest.Get(url); //直接下载不显示进度 yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError) { yield break; } DownloadHandler downloadHandler = request.downloadHandler; if (!downloadHandler.isDone) { Debug.Log("正在下载"); yield return downloadHandler; } else { Debug.Log("下载完成"); byte[] data = request.downloadHandler.data; using (FileStream fs = new FileStream(localpath, FileMode.Create)) { fs.Write(data, 0, data.Length); fs.Close(); if (localpath != "" && localpath != null) { if (File.Exists(localpath)) { this.GetComponent().SetUrl(localpath); this.GetComponent().Play(); } } } } } // Update is called once per frame void Update() { if (rawImage) { rawImage.texture = ap.getTexture(); } if (render) { render.material.mainTexture = ap.getTexture(); } } private void OnDisable() { this.GetComponent().Pause(); } }