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>
|
|||
|
|
/// <20><>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD><D5B9><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public class VideoPlayerEx : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
public static VideoPlayerEx Instance;
|
|||
|
|
|
|||
|
|
private void Awake()
|
|||
|
|
{
|
|||
|
|
Instance = this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public VideoPlayer videoPlayer;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>RectTransform<72><6D><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public RectTransform rectPlayer;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>Ű<EFBFBD>ť
|
|||
|
|
/// </summary>
|
|||
|
|
public Button btnPlay;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>Ű<EFBFBD>ťͼƬ
|
|||
|
|
/// </summary>
|
|||
|
|
public Sprite spPlay;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ͣ<EFBFBD><CDA3>ťͼƬ
|
|||
|
|
/// </summary>
|
|||
|
|
public Sprite spPause;
|
|||
|
|
/// <summary>
|
|||
|
|
/// ȫ<><C8AB><EFBFBD><EFBFBD>ť
|
|||
|
|
/// </summary>
|
|||
|
|
public Button btnFullscreen;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>رհ<D8B1>ť
|
|||
|
|
/// </summary>
|
|||
|
|
public Button btnClose;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ƵƬ<C6B5><C6AC>
|
|||
|
|
/// </summary>
|
|||
|
|
public VideoClip Clip;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ݱ<EFBFBD><DDB1><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public Text txtTitle;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ·<C6B5><C2B7>
|
|||
|
|
/// </summary>
|
|||
|
|
public string URL;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public bool PlayOnAwake;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>Ƶʱ<C6B5><CAB1>
|
|||
|
|
/// </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 <EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ͣ
|
|||
|
|
/// </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>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>
|
|||
|
|
/// </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>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>
|
|||
|
|
/// </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 <EFBFBD><EFBFBD>ť<EFBFBD>¼<EFBFBD>
|
|||
|
|
public void OnBtnCloseClick()
|
|||
|
|
{
|
|||
|
|
Stop();
|
|||
|
|
|
|||
|
|
if (onVideoClosed != null)
|
|||
|
|
{
|
|||
|
|
HideVideoPlayer();
|
|||
|
|
|
|||
|
|
// videoPlayer.GetComponent<RectTransform>().localScale = Vector3.one;
|
|||
|
|
videoPlayer.targetTexture.Release();
|
|||
|
|
|
|||
|
|
onVideoClosed();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD>ȫ<EFBFBD><C8AB>
|
|||
|
|
/// </summary>
|
|||
|
|
bool isFullScreen;
|
|||
|
|
public void OnBtnFullScreenClick()
|
|||
|
|
{
|
|||
|
|
if (isFullScreen)
|
|||
|
|
{
|
|||
|
|
isFullScreen = false;
|
|||
|
|
rectPlayer.localScale = Vector3.one * 0.6f;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
isFullScreen = true;
|
|||
|
|
rectPlayer.localScale = Vector3.one;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
[HideInInspector]
|
|||
|
|
public bool isPlaying;
|
|||
|
|
void OnBtnPlayClick()
|
|||
|
|
{
|
|||
|
|
if (videoPlayer.clip == null && string.IsNullOrEmpty(URL))
|
|||
|
|
{
|
|||
|
|
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƵԴ");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
if (isPlaying)
|
|||
|
|
{
|
|||
|
|
Pause();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Play();
|
|||
|
|
}
|
|||
|
|
btnPlay.GetComponent<Image>().sprite = isPlaying ? spPause : spPlay;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD><C8A1>Ƶʱ<C6B5><CAB1>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public float GetTime()
|
|||
|
|
{
|
|||
|
|
return (videoPlayer.frameCount / videoPlayer.frameRate);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
|
/// </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>
|
|||
|
|
/// <20><>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD>Ӧ
|
|||
|
|
/// </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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|