173 lines
4.7 KiB
C#
173 lines
4.7 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using QFramework;
|
||
using UnityEngine.Video;
|
||
using System;
|
||
using System.Net;
|
||
using UnityEngine.EventSystems;
|
||
using DG.Tweening.Plugins.Options;
|
||
|
||
namespace QFramework.Example
|
||
{
|
||
public class UIVideoData : UIPanelData
|
||
{
|
||
public string url;
|
||
public Vector2 offset;
|
||
public Vector2 size;
|
||
public string finishedEvent;
|
||
public string closeEvent;
|
||
}
|
||
public partial class UIVideo : UIPanel
|
||
{
|
||
string totalTime;
|
||
bool isDragging = false;
|
||
protected override void OnInit(IUIData uiData = null)
|
||
{
|
||
mData = uiData as UIVideoData ?? new UIVideoData();
|
||
TypeEventSystem.Global.Register<OnModuleQuit>((arg) => Close()).UnRegisterWhenGameObjectDestroyed(gameObject);
|
||
// please add init code here
|
||
Progress.onValueChanged.AddListener(OnSliderValueChanged);
|
||
Progress.OnBeginDragEvent(OnProgressBeginDrag);
|
||
Progress.OnEndDragEvent(OnProgressEndDrag);
|
||
player.loopPointReached += VideoPlayer_loopPointReached;
|
||
player.prepareCompleted += OnPrepareCompleted;
|
||
CloseBtn.onClick.AddListener(Close);
|
||
VideoPlayBtn.onClick.AddListener(() =>
|
||
{
|
||
player.Pause();
|
||
SetImg();
|
||
});
|
||
|
||
PauseBtn.onClick.AddListener(() =>
|
||
{
|
||
if (player.isPaused)
|
||
{
|
||
player.Play();
|
||
}
|
||
else
|
||
{
|
||
player.Pause();
|
||
}
|
||
SetImg();
|
||
});
|
||
}
|
||
|
||
public void SetImg()
|
||
{
|
||
if (player.isPaused)
|
||
{
|
||
PauseImg.gameObject.SetActive(true);
|
||
}
|
||
else
|
||
{
|
||
PauseImg.gameObject.SetActive(false);
|
||
}
|
||
}
|
||
|
||
public void Close()
|
||
{
|
||
if (player.isPlaying)
|
||
{
|
||
player.Stop();
|
||
|
||
}
|
||
if (string.IsNullOrEmpty(mData.finishedEvent) == false)
|
||
{
|
||
StringEventSystem.Global.Send(mData.finishedEvent);
|
||
}
|
||
if (string.IsNullOrEmpty(mData.closeEvent) == false)
|
||
{
|
||
StringEventSystem.Global.Send(mData.closeEvent);
|
||
}
|
||
player.targetTexture.Release();
|
||
Hide();
|
||
|
||
}
|
||
|
||
private void OnPrepareCompleted(VideoPlayer source)
|
||
{
|
||
Progress.maxValue = (float)player.length;
|
||
totalTime = Utility.FormatTime(player.length);
|
||
}
|
||
|
||
private void OnProgressEndDrag(PointerEventData data)
|
||
{
|
||
isDragging = false;
|
||
player.Play(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ
|
||
}
|
||
|
||
private void OnProgressBeginDrag(PointerEventData data)
|
||
{
|
||
isDragging = true;
|
||
player.Pause(); // <20><>ͣ<EFBFBD><CDA3>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD>
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>仯ʱ<E4BBAF><CAB1><EFBFBD><EFBFBD>
|
||
/// </summary>
|
||
/// <param name="value"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ</param>
|
||
private void OnSliderValueChanged(float value)
|
||
{
|
||
if (isDragging)
|
||
{
|
||
// <20>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
||
player.time = value;
|
||
}
|
||
}
|
||
void Update()
|
||
{
|
||
if (!isDragging && player.isPlaying)
|
||
{
|
||
// <20><><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ<D6B5><CEAA>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
||
Progress.value = (float)player.time;
|
||
|
||
VideoTimeText.text = $"{Utility.FormatTime(player.time)}/{totalTime}";
|
||
}
|
||
}
|
||
|
||
|
||
|
||
protected override void OnOpen(IUIData uiData = null)
|
||
{
|
||
mData = uiData as UIVideoData ?? new UIVideoData();
|
||
if (mData.size != Vector2.zero)
|
||
{
|
||
VideoContent.rectTransform.sizeDelta = mData.size;
|
||
}
|
||
VideoContent.transform.localPosition = mData.offset;
|
||
string path = Global.videoPath + mData.url;
|
||
if (string.IsNullOrEmpty(path) == false)
|
||
{
|
||
player.url = path;
|
||
player.Play();
|
||
SetImg();
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("û<><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD>Ƶ<EFBFBD><C6B5>" + path);
|
||
}
|
||
}
|
||
|
||
private void VideoPlayer_loopPointReached(UnityEngine.Video.VideoPlayer source)
|
||
{
|
||
if (string.IsNullOrEmpty(mData.finishedEvent) == false)
|
||
{
|
||
StringEventSystem.Global.Send(mData.finishedEvent);
|
||
}
|
||
player.targetTexture.Release();
|
||
}
|
||
|
||
protected override void OnShow()
|
||
{
|
||
}
|
||
|
||
protected override void OnHide()
|
||
{
|
||
}
|
||
|
||
protected override void OnClose()
|
||
{
|
||
}
|
||
}
|
||
}
|