123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- 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<AVProVideoPlayer>().SetUrl(localpath);
- this.GetComponent<AVProVideoPlayer>().Play();
- }
- }
- }
- private void Start()
- {
- localpath = Application.persistentDataPath + "/OOBE/" + url.Split('/')[url.Split('/').Length-1];
- ap = this.GetComponent<AVProVideoPlayer>();
- rawImage = this.GetComponent<RawImage>();
- render = this.GetComponent<MeshRenderer>();
- 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<AVProVideoPlayer>().SetUrl(localpath);
- this.GetComponent<AVProVideoPlayer>().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<AVProVideoPlayer>().Pause();
- }
- }
|