1. 首先先創建一個Render TexTure 在創建一個RawImage,
再把創建好的Render TexTure (我是隨意起的名字,你們自己看着起) ,掛載到 RawImage 的Texture的地方 如下圖所示
2.在創建Video Player組件 把Render Texture 掛上去(掛在Target Texture上面),Video Clip (可以直接把視頻拖上去,也可以寫腳本控制) 如下圖
3.在給滑動條加上Event Trigger 組件,用一個開始拖拽的方法,一個結束拖拽的方法即可
兩個方法在腳本里寫的有,拖拽在上面即可
下面上代碼
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; public class ShuiWenZhiShiButton : MonoBehaviour { public GameObject mv_obj; public VideoClip shipin1,shipin2; public VideoPlayer videoPlayer; public Button play_button; //播放按鈕 public Button pause_button; //暫停按鈕 public Text mv_length_text; //顯示視頻的長度 public Slider slider_kuajin; private bool isDrag; private int hour, minute, second; // 時 分 秒 // Start is called before the first frame update void Start() { Debug.Log (shipin1.length); //按鈕1 button[0].onClick.AddListener(delegate { Photoenlable(0); if (mv_obj.activeSelf==false) { OnorOff_MVjiemian(true); } videoPlayer.clip = shipin1; }); //按鈕2 button[1].onClick.AddListener(delegate { Photoenlable(1); if (mv_obj.activeSelf == false) { OnorOff_MVjiemian(true); } videoPlayer.clip = shipin2; }); //按鈕3 button[2].onClick.AddListener(delegate { Photoenlable(2); }); } //打開或者關閉視頻界面 public void OnorOff_MVjiemian(bool tra) { mv_obj.SetActive(tra); } //視頻播放 public void MV_Play() { play_button.gameObject.SetActive(false); pause_button.gameObject.SetActive(true); videoPlayer.Play(); } //視頻暫停 public void MV_Pause() { play_button.gameObject.SetActive(true); pause_button.gameObject.SetActive(false); videoPlayer.Pause(); } //顯示視頻的時間 public void ShiPin_Length() { hour = (int)videoPlayer.time / 3600; minute = ((int)videoPlayer.time - hour * 3600) / 60; second = (int)videoPlayer.time - hour * 3600 - minute * 60; mv_length_text.text = string.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second); } /// 修改視頻播放進度 private void ChangeVideoPlayTime() { if (isDrag == true) { videoPlayer.time = slider_kuajin.value*videoPlayer.clip.length; } } //修改進度條 public void XiuGaiJinDuTiao() { if (!isDrag) { slider_kuajin.value = (float)(videoPlayer.time / videoPlayer.clip.length); } } //開始拖拽 public void OnDragdrop() { isDrag = true; } //結束拖拽 public void OnEndDrag() { isDrag = false; videoPlayer .time = slider_kuajin.value* videoPlayer.clip.length; } // Update is called once per frame void Update() { if (videoPlayer.clip !=null) { ChangeVideoPlayTime(); XiuGaiJinDuTiao(); ShiPin_Length(); } } }
2.UI擺放結構
腳本掛在父物體上
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; public class Video_Player : MonoBehaviour { public GameObject mv_obj; public VideoClip shipin1; public VideoPlayer videoPlayer; public Button play_button; //播放按鈕
public Button pause_button; //暫停按鈕
public Button exit_button; //關閉界面
public Text mv_length_text; //顯示視頻的長度
public Slider slider_kuajin; private bool isDrag; private int hour, minute, second; // 時 分 秒 // Start is called before the first frame update
void Start() { //videoPlayer = this.GetComponent<VideoPlayer>();
Debug.Log(shipin1.length); videoPlayer.clip = shipin1; videoPlayer.Play(); play_button.onClick.AddListener(MV_Play); pause_button.onClick.AddListener(MV_Pause); exit_button.onClick.AddListener(delegate { OnorOff_MVjiemian(false); }); } //打開或者關閉視頻界面
public void OnorOff_MVjiemian(bool tra) { mv_obj.SetActive(tra); } //視頻播放
public void MV_Play() { play_button.gameObject.SetActive(false); pause_button.gameObject.SetActive(true); videoPlayer.Play(); } //視頻暫停
public void MV_Pause() { play_button.gameObject.SetActive(true); pause_button.gameObject.SetActive(false); videoPlayer.Pause(); } //顯示視頻的時間
public void ShiPin_Length() { hour = (int)videoPlayer.time / 3600; minute = ((int)videoPlayer.time - hour * 3600) / 60; second = (int)videoPlayer.time - hour * 3600 - minute * 60; //mv_length_text.text = string.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second);
} /// 修改視頻播放進度
private void ChangeVideoPlayTime() { if (isDrag == true) { videoPlayer.time = slider_kuajin.value * videoPlayer.clip.length; } } //修改進度條
public void XiuGaiJinDuTiao() { if (!isDrag) { slider_kuajin.value = (float)(videoPlayer.time / videoPlayer.clip.length); } } //開始拖拽
public void OnDragdrop() { isDrag = true; } //結束拖拽
public void OnEndDrag() { isDrag = false; videoPlayer.time = slider_kuajin.value * videoPlayer.clip.length; } // Update is called once per frame
void Update() { if (videoPlayer.clip != null) { ChangeVideoPlayTime(); XiuGaiJinDuTiao(); //ShiPin_Length();
} } }
根據自己需要修改即可,感謝閱讀