欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 旅游 > unity 播放视频可以暂停拖拽进度体

unity 播放视频可以暂停拖拽进度体

2025/6/2 11:57:43 来源:https://blog.csdn.net/my_bzh/article/details/148218121  浏览:    关键词:unity 播放视频可以暂停拖拽进度体

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();}
}

}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词