162 lines
4.4 KiB
C#
Raw Normal View History

2024-12-17 18:07:20 +08:00
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();
// 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(() =>
{
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();
});
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);
}
}
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;
player.url = Global.videoPath + mData.url;
player.Play();
SetImg();
}
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()
{
}
}
}