unity 加载视频可以后台替换
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; public class Main : MonoBehaviour { public GameObject allBtn; public RawImage vidIamge; public VideoPlayer vidPlay; private List<VideoClip> vidClips = new List<VideoClip>(); private string filePath; private FileInfo[] files; void Start() { vidIamge.gameObject.SetActive(false); vidPlay.targetTexture.Release(); filePath = Application.dataPath + "/vid"; if (Directory.Exists(filePath)) {//判断这个路径是否存在 DirectoryInfo direction = new DirectoryInfo(filePath); files = direction.GetFiles("*.mp4", SearchOption.AllDirectories); } for(int i = 0; i < allBtn.transform.childCount; i++) { Button btn = allBtn.transform.GetChild(i).GetComponent<Button>(); EventTriggerListener.Get(btn.gameObject).onClick = onBtn; } EventTriggerListener.Get(vidIamge.gameObject).onClick = onVidIage; } void onBtn(GameObject sender) { vidIamge.gameObject.SetActive(true); for (int i = 0; i < allBtn.transform.childCount; i++) { if (sender.name == allBtn.transform.GetChild(i).name) { string strURL= "file://" + filePath + "/" + files[i].Name; vidPlay.url = strURL; } } } void onVidIage(GameObject sender) { vidPlay.Stop(); vidPlay.targetTexture.Release(); vidIamge.gameObject.SetActive(false); } }
posted on 2021-09-08 14:48 zqiang0803 阅读(193) 评论(0) 编辑 收藏 举报