305 lines
9.0 KiB
C#
Raw Permalink Normal View History

2026-04-24 16:57:44 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2026-05-18 08:42:33 +08:00
using Kill.Core;
2026-04-24 16:57:44 +08:00
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
namespace Kill.UI.Components
{
public class VideoCtrl : MonoBehaviour
2026-05-18 08:42:33 +08:00
{
public bool istest;
public VideoPlayer videoPlayer;
2026-04-24 16:57:44 +08:00
2026-05-18 08:42:33 +08:00
public bool needFullScreen = false;
bool isFullScreen = false;
/// <summary>
/// 0:正常播放 1:全屏播放
/// </summary>
public List<RenderTexture> renderTextures;
public RawImage windowImage;
public RawImage fullImage;
public GameObject playButton;
public GameObject pauseButton;
public Text totalTimeText;
public Text nowTime;
public Slider slider;
bool isplaying = false;
2026-06-08 08:55:10 +08:00
bool ispause = true;
2026-05-18 08:42:33 +08:00
Action playOver;
string url;
2026-06-12 16:07:27 +08:00
public GameObject loading;
2026-05-18 08:42:33 +08:00
private void FixedUpdate()
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
if (isplaying)
{
2026-04-24 16:57:44 +08:00
2026-05-18 08:42:33 +08:00
OnVideoPlaying();
}
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
public void Init(string url, Action playOver)
2026-04-24 16:57:44 +08:00
{
2026-06-12 16:07:27 +08:00
if(loading)
loading.SetActive(true);
2026-06-08 08:55:10 +08:00
nowTime.text=SecondToHour(0);
2026-05-18 08:42:33 +08:00
this.playOver = playOver;
this.url = url;
SetVideo(url);
if (playButton != null)
{
playButton.SetActive(false);
}
if (pauseButton != null)
{
pauseButton.SetActive(false);
}
ispause = true;
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
public void PlayVideo()
{
StartCoroutine(YieldPlay());
2026-04-24 16:57:44 +08:00
2026-05-18 08:42:33 +08:00
}
2026-04-24 16:57:44 +08:00
2026-05-18 08:42:33 +08:00
public IEnumerator YieldPlay()
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
yield return new WaitForSeconds(0.1f);
ispause = false;
videoPlayer.Play();
if (playButton != null)
{
playButton.SetActive(false);
}
if (pauseButton != null)
{
pauseButton.SetActive(true);
}
isplaying = true;
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
public void PauseVideo()
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
ispause = true;
videoPlayer.Pause();
isplaying = false;
if (playButton != null)
{
playButton.SetActive(true);
}
if (pauseButton != null)
{
pauseButton.SetActive(false);
}
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
public void RePlay()
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
videoPlayer.Stop();
videoPlayer.frame = 0;
PlayVideo();
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
public void SetVideo(string url)
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
videoPlayer.url = url;
videoPlayer.prepareCompleted += VideoPrepared;
videoPlayer.Prepare();
if (playButton != null)
{
playButton.SetActive(true);
}
2026-04-24 16:57:44 +08:00
}
2026-06-08 08:55:10 +08:00
public void Stop()
{
videoPlayer.Stop();
videoPlayer.url = "";
nowTime.text=SecondToHour(0);
}
2026-05-18 08:42:33 +08:00
int totalTime = 0;
public void VideoPrepared(VideoPlayer player)
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
if (renderTextures != null && renderTextures.Count > 0)
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
foreach (var r in renderTextures)
Destroy(r);
renderTextures.Clear();
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
renderTextures=new List<RenderTexture>();
RenderTexture texture = new RenderTexture((int)player.width, (int)player.height, 1);
renderTextures.Add(texture);
player.targetTexture = texture;
windowImage.texture = texture;
if (needFullScreen)
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
var size = fullImage.GetComponentInParent<RectTransform>().rect;
RenderTexture fullTexture = new RenderTexture((int)size.height, (int)size.width, 1);
fullImage.GetComponent<RectTransform>().sizeDelta = new Vector2(size.height, size.width);
renderTextures.Add(fullTexture);
fullImage.texture = fullTexture;
}
videoPlayer.frame = 0;
if (slider != null)
{
totalTime = (int)(player.frameCount / player.frameRate + 0.5f);
totalTimeText.text = SecondToHour(totalTime);
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
videoPlayer.prepareCompleted -= VideoPrepared;
player.loopPointReached += VideoOver;
if (playButton != null)
playButton.SetActive(true);
2026-06-08 08:55:10 +08:00
nowTime.text=SecondToHour(0);
2026-06-12 16:07:27 +08:00
if(loading)
loading.SetActive(false);
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
public void ShowAnimation(int dir)
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
Animation Anim = GetComponent<Animation>();
if (Anim != null)
{
if (dir == -1)
{
Anim["视频出现"].time = Anim["视频出现"].clip.length;
Anim["视频出现"].speed = -1;
Anim.Play("视频出现");
}
else
{
Anim["视频出现"].time = 0;
Anim["视频出现"].speed = 1;
Anim.Play("视频出现");
}
}
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
long lastFrame = 0;
/// <summary>
/// 播放中事件(给进度条赋值)
/// </summary>
public void OnVideoPlaying()
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
if (slider == null)
return;
if (Mathf.Abs(videoPlayer.frame - lastFrame) < 1)
return;
slider.value = (videoPlayer.frame + 1) / (float)videoPlayer.frameCount;
lastFrame = videoPlayer.frame;
nowTime.text = SecondToHour((int)(totalTime * slider.value));
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
/// <summary>
/// 拖动进度条
/// </summary>
public void PointDownUp()
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
//if (slider.value != lastValue)
//{
// videoPlayer.frame = (long)(videoPlayer.frameCount * slider.value);
//}
if (slider.value >= 0.98f)
{
videoPlayer.Stop();
VideoOver(videoPlayer);
return;
2026-04-24 16:57:44 +08:00
2026-05-18 08:42:33 +08:00
}
videoPlayer.frame = (long)(videoPlayer.frameCount * slider.value) - 1;
nowTime.text = SecondToHour((int)(totalTime * slider.value));
if (!ispause)
{
videoPlayer.Play();
isplaying = true;
}
// TeacherServer.m_instance.SendVideoProcess((long)(videoPlayer.frameCount * slider.value) - 1);
}
public void PointDown()
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
videoPlayer.Pause();
isplaying = false;
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
public void Draging()
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
videoPlayer.frame = (long)(videoPlayer.frameCount * slider.value) - 1;
nowTime.text = SecondToHour((int)(totalTime * slider.value));
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
public void VideoOver(VideoPlayer vp)
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
vp.loopPointReached -= VideoOver;
isplaying = false;
if (playButton != null)
{
playButton.SetActive(true);
}
if (pauseButton != null)
{
pauseButton.SetActive(false);
}
2026-06-08 08:55:10 +08:00
playOver?.Invoke();
2026-05-18 08:42:33 +08:00
Init(url, playOver);
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
public void SetFullScreen(bool isFullScreen)
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
this.isFullScreen = isFullScreen;
if (isFullScreen)
{
#if UNITY_ANDROID
AndroidStatusBar.statusBarState = AndroidStatusBar.States.Hidden;
#endif
videoPlayer.targetTexture = renderTextures[1];
fullImage.transform.parent.gameObject.SetActive(true);
UIManager.Instance.AddBackAction(() => SetFullScreen(false));
if (ispause)
PlayVideo();
}
else
{
#if UNITY_ANDROID
AndroidStatusBar.statusBarState = AndroidStatusBar.States.TranslucentOverContent;
#endif
videoPlayer.targetTexture = renderTextures[0];
fullImage.transform.parent.gameObject.SetActive(false);
UIManager.Instance.RecoverBackAction();
}
2026-04-24 16:57:44 +08:00
}
2026-05-18 08:42:33 +08:00
/// <summary>
/// 秒转换小时
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public string SecondToHour(float time)
2026-04-24 16:57:44 +08:00
{
2026-05-18 08:42:33 +08:00
int hour = 0;
int minute = 0;
int second = 0;
second = (int)time;
if (second > 60)
{
minute = second / 60;
second = second % 60;
}
if (minute > 60)
{
hour = minute / 60;
minute = minute % 60;
}
string result = "";
if (hour > 0)
{
result += hour + ":";
2026-04-24 16:57:44 +08:00
2026-05-18 08:42:33 +08:00
}
if (minute < 10)
result += "0";
result += minute + ":";
if (second < 10)
result += "0";
result += second.ToString();
return result;
2026-04-24 16:57:44 +08:00
}
}
}