Unity 添加视频和图片在一个文件夹里,可以顺序播放,视频进度条程序可更改
using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using UnityEngine.Video; using System.Collections; using System.Collections.Generic; using System.IO; using System; public class Main : MonoBehaviour { [Header("视频")] public VideoPlayer vid; public Slider mySlider; public GameObject playBtn; public GameObject di; public RawImage vidRawimg; private string playName; // public List<string> vidList = new List<string>(); // public List<string> spList = new List<string>(); public List<string> pathList = new List<string>(); public RawImage Rawimage; public Button nextBtn; public Button prevBtn; private int stepNum = 0; private FileInfo[] floders; private WWW www; private Texture2D texture; private string ifLoaderVid;//是否已经预制加载; private string ifPlusOr;//是增加还是减少 //视频预加载 private string pathStr1; private int lastIndex1; private string sliceJPGOrVid1; private IfMouseMove ifmouse; private void Awake() { string filePath = Application.dataPath + "/Vid"; DirectoryInfo direction = new DirectoryInfo(filePath); floders = direction.GetFiles("*", SearchOption.TopDirectoryOnly); for (int i = 0; i < floders.Length; i++) { // if (i % 2 == 0) { string floderPath = floders[i].ToString(); pathList.Add(floderPath); // } } } private void Start() { mySlider.GetComponent<Slider>().onValueChanged.AddListener(SetValueChange); EventTriggerListener.Get(playBtn).onClick = onPlay; playName = playBtn.GetComponent<Image>().sprite.name; EventTriggerListener.Get(di).onEnter = onEnterFun; EventTriggerListener.Get(mySlider.gameObject).onEnter = onEnterFun; EventTriggerListener.Get(playBtn).onEnter = onEnterFun; EventTriggerListener.Get(di).onExit = onExitFun; playBtn.transform.localScale = new Vector2(0, 1); EventTrigger trigger = mySlider.gameObject.GetComponent<EventTrigger>(); EventTrigger.Entry entry = new EventTrigger.Entry(); // 鼠标点击事件 entry.eventID = EventTriggerType.EndDrag; entry.callback = new EventTrigger.TriggerEvent(); entry.callback.AddListener(OnClick); trigger.triggers.Add(entry); EventTriggerListener.Get(prevBtn.gameObject).onClick = prevFun; EventTriggerListener.Get(nextBtn.gameObject).onClick = nextFun; // image.GetComponent<Image>().sprite = spList[2]; ifmouse = this.GetComponent<IfMouseMove>(); } void nextFun(GameObject sender) { if (stepNum < floders.Length - 1) { ifPlusOr = "plus"; stepNum++; changeFun(); } } void prevFun(GameObject sender) { if (stepNum > 0) { ifPlusOr = "cut"; stepNum--; changeFun(); } } private void changeFun() { ifmouse.timer = 0; playBtn.GetComponent<Image>().sprite = playBtn.GetComponent<InitImage>().stopImage; string pathStr = pathList[stepNum].ToString(); int lastIndex = pathStr.LastIndexOf("."); string sliceJPGOrVid = pathStr.Substring(lastIndex + 1, 1); if (sliceJPGOrVid == "j") { if(ifPlusOr== "plus") { pathStr1 = pathList[stepNum + 1].ToString(); } else if (ifPlusOr == "cut") { pathStr1 = pathList[stepNum - 1].ToString(); } lastIndex1 = pathStr1.LastIndexOf("."); sliceJPGOrVid1 = pathStr1.Substring(lastIndex1 + 1, 1); //判断加载视频 if (sliceJPGOrVid1 == "m") { vid.targetTexture.Release(); vid.url = pathStr1; vid.SetDirectAudioMute(0, true); print("执行预制加载"); ifLoaderVid = "finishLoader"; } Rawimage.transform.parent.localScale = new Vector2(1, 1); www = new WWW(pathStr); texture = www.texture; Rawimage.texture = texture; } else if (sliceJPGOrVid == "m") { if(ifLoaderVid == "finishLoader") { ifLoaderVid = ""; vid.Stop(); vid.Play(); } else { vid.url = pathStr; } Rawimage.transform.parent.localScale = new Vector2(0, 1); vid.SetDirectAudioMute(0, false); } } private void OnClick(BaseEventData pointData) { Debug.Log("Button Clicked. EventTrigger.."); string name = playBtn.GetComponent<Image>().sprite.name; if (name == playName) { vid.Play(); } else { vid.Pause(); } } void moveFun() { float vidLength = (float)vid.length;//视频的总长度 float vidTime = (float)vid.time;//视频的当前时长 mySlider.value = vidTime / vidLength; } private void Update() { if (vid.isPlaying) { moveFun(); if (vid.length - vid.time <= 0.05) { vid.Stop(); vid.Play(); } } if (Input.GetKeyDown(KeyCode.DownArrow)|| Input.GetKeyDown(KeyCode.RightArrow)) { if (stepNum < floders.Length -1) { ifPlusOr = "plus"; stepNum++; changeFun(); } } else if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.LeftArrow)) { if (stepNum > 0) { ifPlusOr = "cut"; stepNum--; changeFun(); } } } private void SetValueChange(float value) { vid.time = mySlider.value * vid.length; } void onPlay(GameObject sender) { string name = playBtn.GetComponent<Image>().sprite.name; if (name == playName) { playBtn.GetComponent<Image>().sprite = playBtn.GetComponent<InitImage>().playImage; vid.Pause(); } else { playBtn.GetComponent<Image>().sprite = playBtn.GetComponent<InitImage>().stopImage; vid.Play(); } } void onEnterFun(GameObject sender) { playBtn.transform.localScale = new Vector2(1, 1); } void onExitFun(GameObject sender) { playBtn.transform.localScale = new Vector2(0, 1); } }
posted on 2022-01-18 11:02 zqiang0803 阅读(229) 评论(0) 编辑 收藏 举报