Unity实现视频拖动条,,暂停按钮,开始按钮,当前时间控制

using System;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using System.Collections;

public class VideoController1 : MonoBehaviour
{
    public VideoPlayer videoPlayer;
    public Slider sliderVideo;
    public Text currentTimeText;
    public Text totalTimeText;
    public Button playButton;
    public Button pauseButton;

    private bool isDraggingSlider = false;

    private void Start()
    {
        if (videoPlayer == null)
        {
            Debug.LogError("VideoPlayer component is missing!");
            return;
        }

        videoPlayer.prepareCompleted += VideoPrepareCompleted;
        videoPlayer.loopPointReached += OnVideoEnd;
        sliderVideo.onValueChanged.AddListener(OnSliderValueChange);

        playButton.onClick.AddListener(OnClickPlay);
        pauseButton.onClick.AddListener(OnClickPause);

        videoPlayer.Prepare();

    }

    public void OpenVideo() {

    }


    private void VideoPrepareCompleted(VideoPlayer source)
    {
        sliderVideo.maxValue = (float)videoPlayer.length;
        UpdateTimeTexts(0f);
    }

    private void OnSliderValueChange(float value)
    {
        if (!isDraggingSlider)
        {
            videoPlayer.time = value;
            UpdateTimeTexts(value);
        }
    }

    private void UpdateTimeTexts(float time)
    {
        int minutes = Mathf.FloorToInt(time / 60f);
        int seconds = Mathf.FloorToInt(time % 60f);
        currentTimeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);

        int totalMinutes = Mathf.FloorToInt((float)videoPlayer.length / 60f);
        int totalSeconds = Mathf.FloorToInt((float)videoPlayer.length % 60f);
        totalTimeText.text = string.Format("{0:00}:{1:00}", totalMinutes, totalSeconds);
    }

    private void OnClickPlay()
    {
        videoPlayer.Play();
        playButton.gameObject.SetActive(false);
        pauseButton.gameObject.SetActive(true);
    }

    private void OnClickPause()
    {
        videoPlayer.Pause();
        playButton.gameObject.SetActive(true);
        pauseButton.gameObject.SetActive(false);
    }

    private void OnVideoEnd(VideoPlayer vp)
    {
        videoPlayer.Stop();
        playButton.gameObject.SetActive(true);
        pauseButton.gameObject.SetActive(false);
        UpdateTimeTexts(0f);
    }

    private void Update()
    {
        if (videoPlayer.isPlaying && !isDraggingSlider)
        {
            sliderVideo.value = (float)videoPlayer.time;
            UpdateTimeTexts((float)videoPlayer.time);
        }
    }

    public void BeginDraggingSlider()
    {
        isDraggingSlider = true;
    }

    public void EndDraggingSlider()
    {
        isDraggingSlider = false;
        videoPlayer.time = sliderVideo.value;
        UpdateTimeTexts((float)videoPlayer.time);
    }

    public void PlayVideo(VideoPlayer videoPlayer, VideoClip videoClip, Action videoStartCallback = null, Action videoEndCallback = null)
    {
        if (videoClip == null)
        {
            Debug.LogError("VideoClip is null. Please assign a valid VideoClip.");
            return;
        }

        if (videoPlayer == null)
        {
            videoPlayer = gameObject.AddComponent();
            videoPlayer.playOnAwake = false;
            videoPlayer.waitForFirstFrame = true;
        }

        videoPlayer.clip = videoClip;
        videoPlayer.Play();

        videoStartCallback?.Invoke();
        StartCoroutine(WaitForVideoEnd(videoPlayer, videoEndCallback));
    }


    private IEnumerator WaitForVideoEnd(VideoPlayer videoPlayer, Action action = null)
    {
        while (videoPlayer.isPlaying)
        {
            yield return null;
        }

        yield return new WaitForSeconds((float)videoPlayer.clip.length);

        action?.Invoke();

    }
}

你可能感兴趣的:(unity,游戏引擎,Unity工具)