說明
??最近開發時需要播放視頻,回憶以前做過的專案,好多都有在Unity3D中播放視頻,每次都是自己重新實作一些功能,類似與進度條拖動,顯示時長等等,今天就是將其封裝一下,以后再用到直接拿來用,
包含的功能:
- 視頻進度條,可點擊進度條,拖動進度條
- 點擊視頻實作視頻播放與停止
- 全屏顯示
- 靜音
- 顯示視頻時長
播放界面:

腳本掛載:

EventTrigger:

全屏按鈕和靜音按鈕同理:

主要腳本:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class HK_VideoCtrl : MonoBehaviour
{
[Header("VideoPlayer組件:")]
public VideoPlayer videoPlayer;
[Header("視頻拖動條:")]
public Slider video_Slider;
[Header("VideoBG_TRF組件:")]
public RectTransform VideoBG_TRF;
[Header("視頻播放提示:")]
public Image pause_Img;
[Header("視頻關閉按鈕:")]
public Button close_Btn;
//滑鼠抬起或按下
bool mouseUp = true;
bool isPause = false;
[Header("全屏按鈕:")]
public Button fullScreen_Btn;
[Header("全屏按鈕精靈:")]
public Sprite fullScreen_Sprite;
[Header("非全屏按鈕精靈:")]
public Sprite notFullScreen_Sprite;
[Header("全屏按鈕提示文本:")]
public Text fullScreen_Txt;
bool isFullScreen = false;
[Header("靜音按鈕:")]
public Button mute_Btn;
[Header("靜音按鈕精靈:")]
public Sprite mute_Sprite;
[Header("非靜音按鈕精靈:")]
public Sprite notMute_Sprite;
[Header("靜音按鈕提示文本:")]
public Text mute_Txt;
bool isMute = false;
[Header("視頻時長:")]
public Text videoTime_Txt;
//視頻時長
int clipHour, clipMinute, clipSecond, currentHour, currentMinute, currentSecond;
void Start()
{
close_Btn.onClick.AddListener(OnClickCloseBtn);
video_Slider.onValueChanged.AddListener( SliderValueChangeEvent);
fullScreen_Btn.onClick.AddListener(OnClickFullScreenBtn);
mute_Btn.onClick.AddListener(OnClickMuteBtn);
videoPlayer.targetTexture.Release();
isFullScreen = false;
isMute = false;
clipMinute = (int)(videoPlayer.clip.length) / 60;
clipSecond = (int)(videoPlayer.clip.length - clipMinute * 60);
}
void OnEnable()
{
pause_Img.gameObject.SetActive(false);
}
void Update()
{
SetVideoTime();
}
void FixedUpdate()
{
if (mouseUp)
video_Slider.value = videoPlayer.frame / (videoPlayer.frameCount*1.0f);
}
//設定視頻時間
void SetVideoTime()
{
currentMinute = (int)(videoPlayer.time) / 60;
currentSecond = (int)(videoPlayer.time - currentMinute * 60);
videoTime_Txt.text = string.Format("{0:D2}:{1:D2} / {2:D2}:{3:D2}", currentMinute, currentSecond, clipMinute, clipSecond);
}
//關閉視頻面板
void OnClickCloseBtn()
{
gameObject.SetActive(false);
fullScreen_Txt.text = "全 屏";
fullScreen_Btn.GetComponent<Image>().sprite = fullScreen_Sprite;
VideoBG_TRF.sizeDelta = new Vector2(1511, 950);
isFullScreen = false;
videoPlayer.SetDirectAudioMute(0, false);
mute_Btn.GetComponent<Image>().sprite = notMute_Sprite;
mute_Txt.text = "靜 音";
isMute = false;
}
//全屏
void OnClickFullScreenBtn()
{
if (isFullScreen)
{
VideoBG_TRF.sizeDelta = new Vector2(1511,950);
fullScreen_Btn.GetComponent<Image>().sprite = fullScreen_Sprite;
fullScreen_Txt.text = "全 屏";
}
else
{
VideoBG_TRF.sizeDelta = new Vector2(Screen.width, Screen.height);
fullScreen_Btn.GetComponent<Image>().sprite = notFullScreen_Sprite;
fullScreen_Txt.text = "取消全屏";
}
isFullScreen = !isFullScreen;
}
//靜音
void OnClickMuteBtn()
{
if (isMute)
{
videoPlayer.SetDirectAudioMute(0, false);
mute_Btn.GetComponent<Image>().sprite = notMute_Sprite;
mute_Txt.text = "靜 音";
}
else
{
videoPlayer.SetDirectAudioMute(0, true);
mute_Btn.GetComponent<Image>().sprite = mute_Sprite;
mute_Txt.text = "取消靜音";
}
isMute = !isMute;
}
#region EventTrigger相關事件
//滑鼠按下
public void PointDown()
{
videoPlayer.Pause();
videoPlayer.frame = long.Parse((video_Slider.value * videoPlayer.frameCount).ToString("0."));
mouseUp = false;
isPause = false;
}
//滑鼠抬起
public void PointUp()
{
videoPlayer.Play();
mouseUp = true;
pause_Img.gameObject.SetActive(false);
}
//拖動開始
public void PointDragBegin()
{
mouseUp = false;
}
//拖動結束
public void PointDragEnd()
{
mouseUp = true;
}
//設定視頻暫停
public void SetPauseImg()
{
isPause = !isPause;
pause_Img.gameObject.SetActive(isPause);
if (!isPause)
videoPlayer.Play();
else
videoPlayer.Pause();
}
#endregion
void SliderValueChangeEvent(float value)
{
if (!mouseUp)
{
videoPlayer.frame = long.Parse((value * videoPlayer.frameCount).ToString("0."));
}
}
}
滑鼠移入全屏或靜音按鈕小標簽提示腳本:
using UnityEngine;
public class TipCtrl : MonoBehaviour
{
[Header("提示標簽:")]
public GameObject tip;
private void Start()
{
tip.SetActive(false);
}
public void OnPointEnter()
{
tip.SetActive(true);
}
public void onPointExit()
{
tip.SetActive(false);
}
}
如有問題或者好的方法,歡迎評論,
下載地址
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/300882.html
標籤:其他
下一篇:零基礎Unity做一個中秋詩詞鑒賞網頁,提前祝您中秋快樂!(DoTween影片 | WebGL視頻 | 大檔案上傳GitHub)
