using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using UnityEngine.Events;
using System;
public class VideoPanel : BasePanel
{
private bool isOpen;
private Text title;
private Button close;
private VideoPlayer video;
private RawImage conImage;
private AudioSource audiosource;
private Slider slider;
private Slider setSlider;
private Text currentTimeText;
private Text allTimeText;
private Button pauseBtn;
private Button playBtn;
//视频单位时间 归一划 范围(0-1)
private float NormalizedTime
{
get {
if (Duration == 0)
{
return 0;
}
return (float)(video.time / Duration);
}
}
//总时间
private ulong Duration
{
get
{
if (video.frameRate==0)
{
return 0;
}
return video.frameCount / (ulong)video.frameRate;
}
}
// Update is called once per frame
void Update()
{
if (isOpen == true)
{
slider.value = NormalizedTime;
currentTimeText.text = GetTime((float)video.time);
}
}
private void SliderValueChanged(float _value)
{
setSlider.transform.Find("HandleSlideArea/Handle").GetComponent<Image>().enabled = true;
Seek(_value);
}
//设置视频进度
private void Seek(float _value)
{
_value = Mathf.Clamp(_value, 0, 1);
video.time = _value * Duration;
}
private string GetTime(float _time)
{
int tempTime = (int)_time;
int min = tempTime / 60;
int sec = tempTime % 60;
if (min >= 10)
{
if (sec >= 10)
{
return min.ToString() + ":" + sec.ToString();
}
else
{
return min.ToString() + ":" + "0" + sec.ToString();
}
}
else
{
if (sec >= 10)
{
return "0" + min.ToString() + ":" + sec.ToString();
}
else
{
return "0" + min.ToString() + ":" + "0" + sec.ToString();
}
}
}
public override void OnRegister()
{
base.OnRegister();
isOpen = false;
title = GetObj<Text>("back/Head/Text");
close = GetObj<Button>("back/Head/close");
video = GetObj<VideoPlayer>("back/content");
conImage = GetObj<RawImage>("back/content");
audiosource = GetObj<AudioSource>("back/content");
slider = GetObj<Slider>("back/content/controller/Slider");
setSlider = GetObj<Slider>("back/content/controller/SetSlider");
setSlider.onValueChanged.AddListener(delegate (float _value) { SliderValueChanged(_value); });
currentTimeText = GetObj<Text>("back/content/controller/currentTime");
allTimeText = GetObj<Text>("back/content/controller/allTime");
pauseBtn = GetObj<Button>("back/content/controller/pauseBtn");
playBtn = GetObj<Button>("back/content/controller/playBtn");
OnRigisterButtonVoid(pauseBtn, delegate ()
{
//点击暂停按钮-播放
video.Play();
pauseBtn.gameObject.SetActive(false);
playBtn.gameObject.SetActive(true);
});
OnRigisterButtonVoid(playBtn, delegate ()
{
//点击播放按钮-暂停
video.Pause();
pauseBtn.gameObject.SetActive(true);
playBtn.gameObject.SetActive(false);
});
OnRigisterButtonVoid(close, delegate ()
{
OnExit();
MainManger.intance.Management(MainManger.MainTask.关闭视频事件, null);
});
title.text = "视频资源";
OnExit();
}
// Start is called before the first frame update
IEnumerator PlayVideo(string _path)
{
//videoplay自带代码包,加载目标视频
video.url = _path;
//video.audio = audiosource;
video.Prepare();
WaitForSeconds waitForSeconds = new WaitForSeconds(1);
while (!video.isPrepared)
{
yield return waitForSeconds;
break;
}
//将videoplayer的视频赋予为目标对象的材质(平面是平面播放,球体是球形播放....)
conImage.texture = video.texture;
//开始播放视频和音效
video.Play();
allTimeText.text = GetTime((float)Duration);
video.isLooping = true;
audiosource.Play();
}
public override void OnEnter(object obj)
{
base.OnEnter(obj);
isOpen = true;
Teachs teach = (Teachs)obj;
title.text = teach.resourceName;
StopAllCoroutines();
StartCoroutine(PlayVideo(MainManger.intance.loaclPath + teach.resourcePath));
//MainManger.intance.souceManger.Management(SouceManger.SouceTask.关闭背景音乐,null);
MainManger.intance.Management(MainManger.MainTask.打开视频事件,null);
}
public override void OnUpdate(string content)
{
base.OnUpdate(content);
}
public override void OnExit()
{
base.OnExit();
isOpen = false;
StopAllCoroutines();
video.Stop();
}
}
效果展示:
拖拽中:
Unity版本:2017.4.24(没有用到任何外部插件,应该兼容大部分版本)
源码下载链接:https://download.csdn.net/download/qq_37114869/34412642