using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.UI; using UnityEngine.Video; /// /// 视频播放器扩展组件 /// public class VideoPlayerEx : MonoBehaviour { public static VideoPlayerEx Instance; private void Awake() { Instance = this; } /// /// 视频播放组件 /// public VideoPlayer videoPlayer; /// /// 播放面板RectTransform组件 /// public RectTransform rectPlayer; /// /// 播放按钮 /// public Button btnPlay; /// /// 播放按钮图片 /// public Sprite spPlay; /// /// 暂停按钮图片 /// public Sprite spPause; /// /// 全屏按钮 /// public Button btnFullscreen; /// /// 关闭按钮 /// public Button btnClose; /// /// 视频片段 /// public VideoClip Clip; /// /// 数据标题 /// public Text txtTitle; /// /// 播放视频路径 /// public string URL; /// /// 是否自动播放 /// public bool PlayOnAwake; /// /// 视频时长 /// [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().sprite = spPause; isPlaying = true; } anim = GetComponent(); } #region 视频控制 /// /// 暂停 /// public void Pause() { videoPlayer.Pause(); btnPlay.GetComponent().sprite = spPlay; isPlaying = false; } /// /// 停止 /// public void Stop() { videoPlayer.Stop(); btnPlay.GetComponent().sprite = spPlay; isPlaying = false; } /// /// 播放 /// 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().sprite = spPause; isPlaying = true; } public void HideVideo() { if (isFullScreen) OnBtnFullScreenClick(); HideComplete(); } void HideComplete() { gameObject.SetActive(false); Stop(); IsShow = false; } /// /// 播放 /// 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().sprite = spPlay; isPlaying = false; if(onVideoPlayEnd != null) { onVideoPlayEnd(); } } #endregion #region 按钮事件 public void OnBtnCloseClick() { Stop(); if (onVideoClosed != null) { HideVideoPlayer(); // videoPlayer.GetComponent().localScale = Vector3.one; videoPlayer.targetTexture.Release(); onVideoClosed(); } } /// /// 是否全屏 /// bool isFullScreen; public void OnBtnFullScreenClick() { if (isFullScreen) { isFullScreen = false; rectPlayer.localScale = Vector3.one * 0.6f; } else { isFullScreen = true; rectPlayer.localScale = Vector3.one; } } /// /// 是否正在播放 /// [HideInInspector] public bool isPlaying; void OnBtnPlayClick() { if (videoPlayer.clip == null && string.IsNullOrEmpty(URL)) { Debug.Log("请设置视频源"); } else { if (isPlaying) { Pause(); } else { Play(); } btnPlay.GetComponent().sprite = isPlaying ? spPause : spPlay; } } #endregion #region 进度条控制 /// /// 获取视频时长 /// /// public float GetTime() { return (videoPlayer.frameCount / videoPlayer.frameRate); } /// /// 设置播放时间点 /// /// 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; /// /// 视频自适应 /// 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().localScale = new Vector2(width / maxWidth, 1); } else { videoPlayer.GetComponent().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); } } }