using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using UnityEngine.EventSystems;
public class RelaxInterfaceScript : UIBase
{
// 视频播放控制
public VideoPlayer videoPlayer; // 视频播放器组件
public Slider progressSlider; // 播放进度条
public Text currentTimeText; // 当前播放时间文本
public Text totalTimeText; // 总时长文本
public Button playPauseButton; // 播放/暂停按钮// 界面面板
public GameObject videoPlayPanel; // 视频播放面板
public GameObject videoSelectPanel; // 视频选择面板
public Button quitVideoButton; // 退出视频按钮public static RelaxInterfaceScript Instance; // 单例实例private bool isDraggingProgress = false; // 是否正在拖动进度条
private bool isVideoPrepared = false; // 视频是否已准备就绪void Start()
{Instance = this;// 退出视频按钮点击事件quitVideoButton.onClick.AddListener(() => {videoPlayPanel.SetActive(false);videoSelectPanel.SetActive(true);videoPlayer.Stop();});// 初始化视频播放器if (videoPlayer != null){videoPlayer.playOnAwake = false;videoPlayer.prepareCompleted += OnVideoPrepared;}// 设置进度条回调progressSlider.onValueChanged.AddListener(OnProgressSliderChanged);playPauseButton.onClick.AddListener(TogglePlayPause);// 初始化进度条范围progressSlider.minValue = 0;progressSlider.maxValue = 1;progressSlider.value = 0;var sliderEvents = progressSlider.gameObject.AddComponent<SliderEventTrigger>();sliderEvents.OnBeginDragEvent += () => isDraggingProgress = true;sliderEvents.OnEndDragEvent += () => {isDraggingProgress = false;if (isVideoPrepared){videoPlayer.time = progressSlider.value * videoPlayer.length;}};}void Update()
{if (!isVideoPrepared) return;// 只有在非拖动状态才更新进度条位置if (!isDraggingProgress && videoPlayer.isPlaying){float progress = (float)(videoPlayer.time / videoPlayer.length);progressSlider.value = progress;currentTimeText.text = FormatTime(videoPlayer.time);}// 拖动时只更新时间显示if (isDraggingProgress){currentTimeText.text = FormatTime(progressSlider.value * videoPlayer.length);}
}// 播放指定视频
public void PlayVideo(string videoName)
{if (string.IsNullOrEmpty(videoName)){Debug.LogError("Video name is empty!");return;}videoPlayPanel.SetActive(true);videoSelectPanel.SetActive(false);string videoPath = System.IO.Path.Combine(Application.streamingAssetsPath,"Video",$"{videoName}.mp4").Replace('\\', '/');if (!System.IO.File.Exists(videoPath)){Debug.LogError($"Video file not found: {videoPath}");return;}videoPlayer.source = VideoSource.Url;videoPlayer.url = videoPath;videoPlayer.Prepare();
}// 视频准备完成回调
private void OnVideoPrepared(VideoPlayer player)
{isVideoPrepared = true;totalTimeText.text = FormatTime(player.length);player.Play();
}// 进度条值改变回调
private void OnProgressSliderChanged(float value)
{if (!isVideoPrepared) return;if (isDraggingProgress){currentTimeText.text = FormatTime(value * videoPlayer.length);}
}// 开始拖动进度条
public void OnBeginDragProgress()
{isDraggingProgress = true;
}// 结束拖动进度条
public void OnEndDragProgress()
{isDraggingProgress = false;videoPlayer.time = progressSlider.value * videoPlayer.length;
}// 切换播放/暂停状态
private void TogglePlayPause()
{if (!isVideoPrepared) return;if (videoPlayer.isPlaying){videoPlayer.Pause();}else{videoPlayer.Play();}
}// 格式化时间显示(MM:SS)
private string FormatTime(double timeInSeconds)
{System.TimeSpan time = System.TimeSpan.FromSeconds(timeInSeconds);return string.Format("{0:D2}:{1:D2}", time.Minutes, time.Seconds);
}void OnDestroy()
{// 注销事件监听if (videoPlayer != null){videoPlayer.prepareCompleted -= OnVideoPrepared;}
}private class SliderEventTrigger : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{public System.Action OnBeginDragEvent;public System.Action OnEndDragEvent;public void OnBeginDrag(PointerEventData eventData){OnBeginDragEvent?.Invoke();}public void OnEndDrag(PointerEventData eventData){OnEndDragEvent?.Invoke();}
}
}