363 lines
7.8 KiB
C#
363 lines
7.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Video;
|
|
|
|
/// <summary>
|
|
/// 视频播放器扩展组件
|
|
/// </summary>
|
|
public class VideoPlayerEx : MonoBehaviour
|
|
{
|
|
public static VideoPlayerEx Instance;
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 视频播放组件
|
|
/// </summary>
|
|
public VideoPlayer videoPlayer;
|
|
/// <summary>
|
|
/// 播放面板RectTransform组件
|
|
/// </summary>
|
|
public RectTransform rectPlayer;
|
|
/// <summary>
|
|
/// 播放按钮
|
|
/// </summary>
|
|
public Button btnPlay;
|
|
/// <summary>
|
|
/// 播放按钮图片
|
|
/// </summary>
|
|
public Sprite spPlay;
|
|
/// <summary>
|
|
/// 暂停按钮图片
|
|
/// </summary>
|
|
public Sprite spPause;
|
|
/// <summary>
|
|
/// 全屏按钮
|
|
/// </summary>
|
|
public Button btnFullscreen;
|
|
/// <summary>
|
|
/// 关闭按钮
|
|
/// </summary>
|
|
public Button btnClose;
|
|
/// <summary>
|
|
/// 视频片段
|
|
/// </summary>
|
|
public VideoClip Clip;
|
|
/// <summary>
|
|
/// 数据标题
|
|
/// </summary>
|
|
public Text txtTitle;
|
|
/// <summary>
|
|
/// 播放视频路径
|
|
/// </summary>
|
|
public string URL;
|
|
/// <summary>
|
|
/// 是否自动播放
|
|
/// </summary>
|
|
public bool PlayOnAwake;
|
|
/// <summary>
|
|
/// 视频时长
|
|
/// </summary>
|
|
[HideInInspector]
|
|
public float VideoTime;
|
|
|
|
public event UnityAction onVideoClosed;
|
|
public event UnityAction onVideoPlayEnd;
|
|
|
|
public bool IsShow;
|
|
|
|
public float VideoProgress
|
|
{
|
|
get
|
|
{
|
|
return float.Parse(videoPlayer.frame.ToString())
|
|
/ float.Parse(videoPlayer.frameCount.ToString());
|
|
}
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
if (btnPlay != null)
|
|
btnPlay.onClick.AddListener(OnBtnPlayClick);
|
|
if (btnFullscreen != null)
|
|
btnFullscreen.onClick.AddListener(OnBtnFullScreenClick);
|
|
if (btnClose != null)
|
|
btnClose.onClick.AddListener(OnBtnCloseClick);
|
|
|
|
videoPlayer.playOnAwake = PlayOnAwake;
|
|
videoPlayer.clip = Clip;
|
|
if (!string.IsNullOrEmpty(videoPlayer.url))
|
|
{
|
|
videoPlayer.url = URL;
|
|
}
|
|
|
|
if (videoPlayer.playOnAwake && (videoPlayer.clip != null || !string.IsNullOrEmpty(videoPlayer.url)))
|
|
{
|
|
btnPlay.GetComponent<Image>().sprite = spPause;
|
|
isPlaying = true;
|
|
}
|
|
|
|
anim = GetComponent<Animator>();
|
|
}
|
|
|
|
#region 视频控制
|
|
/// <summary>
|
|
/// 暂停
|
|
/// </summary>
|
|
public void Pause()
|
|
{
|
|
videoPlayer.Pause();
|
|
|
|
btnPlay.GetComponent<Image>().sprite = spPlay;
|
|
|
|
isPlaying = false;
|
|
}
|
|
/// <summary>
|
|
/// 停止
|
|
/// </summary>
|
|
public void Stop()
|
|
{
|
|
videoPlayer.Stop();
|
|
|
|
btnPlay.GetComponent<Image>().sprite = spPlay;
|
|
|
|
isPlaying = false;
|
|
}
|
|
/// <summary>
|
|
/// 播放
|
|
/// </summary>
|
|
void Play()
|
|
{
|
|
if (!videoPlayer.isPaused)
|
|
{
|
|
if (!string.IsNullOrEmpty(URL))
|
|
{
|
|
videoPlayer.url = URL;
|
|
}
|
|
|
|
videoPlayer.loopPointReached += VideoPlayer_loopPointReached;
|
|
videoPlayer.prepareCompleted += VideoPlayer_prepareCompleted;
|
|
}
|
|
videoPlayer.targetTexture.Release();
|
|
videoPlayer.Play();
|
|
|
|
btnPlay.GetComponent<Image>().sprite = spPause;
|
|
|
|
isPlaying = true;
|
|
}
|
|
public void HideVideo()
|
|
{
|
|
if (isFullScreen)
|
|
OnBtnFullScreenClick();
|
|
|
|
HideComplete();
|
|
}
|
|
|
|
void HideComplete()
|
|
{
|
|
gameObject.SetActive(false);
|
|
Stop();
|
|
IsShow = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 播放
|
|
/// </summary>
|
|
public void Play(string url)
|
|
{
|
|
if (videoPlayer.isPlaying || videoPlayer.isPaused)
|
|
{
|
|
Stop();
|
|
}
|
|
URL = url;
|
|
videoPlayer.url = URL;
|
|
Play();
|
|
}
|
|
|
|
void VideoPlayer_prepareCompleted(VideoPlayer source)
|
|
{
|
|
VideoTime = GetTime();
|
|
|
|
FitVideoSize();
|
|
}
|
|
|
|
void VideoPlayer_loopPointReached(VideoPlayer source)
|
|
{
|
|
btnPlay.GetComponent<Image>().sprite = spPlay;
|
|
isPlaying = false;
|
|
|
|
if(onVideoPlayEnd != null)
|
|
{
|
|
onVideoPlayEnd();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 按钮事件
|
|
public void OnBtnCloseClick()
|
|
{
|
|
Stop();
|
|
|
|
if (onVideoClosed != null)
|
|
{
|
|
HideVideoPlayer();
|
|
|
|
// videoPlayer.GetComponent<RectTransform>().localScale = Vector3.one;
|
|
videoPlayer.targetTexture.Release();
|
|
|
|
onVideoClosed();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否全屏
|
|
/// </summary>
|
|
bool isFullScreen;
|
|
public void OnBtnFullScreenClick()
|
|
{
|
|
if (isFullScreen)
|
|
{
|
|
isFullScreen = false;
|
|
rectPlayer.localScale = Vector3.one * 0.6f;
|
|
}
|
|
else
|
|
{
|
|
isFullScreen = true;
|
|
rectPlayer.localScale = Vector3.one;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否正在播放
|
|
/// </summary>
|
|
[HideInInspector]
|
|
public bool isPlaying;
|
|
void OnBtnPlayClick()
|
|
{
|
|
if (videoPlayer.clip == null && string.IsNullOrEmpty(URL))
|
|
{
|
|
Debug.Log("请设置视频源");
|
|
}
|
|
else
|
|
{
|
|
if (isPlaying)
|
|
{
|
|
Pause();
|
|
}
|
|
else
|
|
{
|
|
Play();
|
|
}
|
|
btnPlay.GetComponent<Image>().sprite = isPlaying ? spPause : spPlay;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 进度条控制
|
|
/// <summary>
|
|
/// 获取视频时长
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public float GetTime()
|
|
{
|
|
return (videoPlayer.frameCount / videoPlayer.frameRate);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置播放时间点
|
|
/// </summary>
|
|
/// <param name="time"></param>
|
|
public void SetTime(float time)
|
|
{
|
|
videoPlayer.frame = (long)(time * videoPlayer.frameRate);
|
|
Play();
|
|
}
|
|
#endregion
|
|
|
|
public void PlayVideo(string videoName, string title = null)
|
|
{
|
|
IsShow = true;
|
|
|
|
txtTitle.text = title;
|
|
|
|
URL = CommonData.VideoFullPath + videoName;
|
|
|
|
if (H5Controller.GetPlatformType() == 0 || H5Controller.GetPlatformType() == 1)
|
|
{
|
|
Play(URL);
|
|
}
|
|
}
|
|
|
|
public RawImage rawImage;
|
|
/// <summary>
|
|
/// 视频自适应
|
|
/// </summary>
|
|
void FitVideoSize()
|
|
{
|
|
float maxWidth = rawImage.rectTransform.rect.width;
|
|
float maxHeight = rawImage.rectTransform.rect.height;
|
|
// Debug.Log(maxWidth + "," + maxHeight);
|
|
float width, height;
|
|
float tw = videoPlayer.width;
|
|
float th = videoPlayer.height;
|
|
// Debug.Log(tw + "," + th);
|
|
width = maxWidth;
|
|
height = th * maxWidth / tw;
|
|
if (height > maxHeight)
|
|
{
|
|
height = maxHeight;
|
|
width = tw * maxHeight / th;
|
|
|
|
videoPlayer.GetComponent<RectTransform>().localScale = new Vector2(width / maxWidth, 1);
|
|
}
|
|
else
|
|
{
|
|
videoPlayer.GetComponent<RectTransform>().localScale = new Vector2(1, height/ maxHeight);
|
|
}
|
|
// Debug.Log(width + "," + height);
|
|
}
|
|
|
|
Animator anim;
|
|
public void ShowVideoPlayer()
|
|
{
|
|
if (anim != null)
|
|
{
|
|
anim.SetTrigger("Show");
|
|
}
|
|
|
|
AudioManager.Instance.PauseBackgroundMusic();
|
|
}
|
|
|
|
public void HideVideoPlayer()
|
|
{
|
|
if (anim != null)
|
|
{
|
|
anim.SetTrigger("Hide");
|
|
}
|
|
|
|
AudioManager.Instance.ResumeBackgroundMusic();
|
|
}
|
|
|
|
public GameObject obj;
|
|
public bool isDrag;
|
|
|
|
public void ShowProgress()
|
|
{
|
|
obj.SetActive(true);
|
|
|
|
Invoke("HideProgress", 3f);
|
|
}
|
|
public void HideProgress()
|
|
{
|
|
if (!isDrag)
|
|
{
|
|
obj.SetActive(false);
|
|
}
|
|
}
|
|
|
|
} |