unity内置播放器的进度条/音量/暂停功能集成

先创建两个滚动条,分别控制进度条和音量

创建一个按钮,控制播放与暂停

给视频播放器挂这个代码:

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

public class Videos : MonoBehaviour
{
    public GameObject videoplayer;//视频播放器
    public GameObject scroolbar;//进度条
    public GameObject volumebar;//音量条
    public bool Changetime;
    public GameObject Stopbtn;
    public int stoptime;
    public Vector2 lastPos;//鼠标上次位置
    public Vector2 currPos;//鼠标当前位置
    private void OnMouseDown()
    {
        
    }
    private void Update()//计算鼠标移动距离
    {
        if (Input.GetMouseButtonDown(0))
        {
            lastPos = Input.mousePosition;
            Stopbtn.GetComponent().alpha = 1;
            stoptime = 50;//多久后开始变透明
        }

        if (Input.GetMouseButtonUp(0))
        {
            currPos = Input.mousePosition;
        }
        //Debug.Log(videoplayer.GetComponent().time);//现在时间
        videoplayer.GetComponent().SetDirectAudioVolume(0,volumebar.GetComponent().value);
    }
    private void FixedUpdate()
    {
        stoptime--;
        if (stop.Ispaused == false&&stoptime<=0)
        {
            Stopbtn.GetComponent().alpha -= 0.04f;
        }
        if (!Input.GetMouseButton(0))
        {
            Debug.Log("click");
            
            if ((float)(videoplayer.GetComponent().time * 30) / (float)videoplayer.GetComponent().frameCount >= 0f && (float)(videoplayer.GetComponent().time * 30) / (float)videoplayer.GetComponent().frameCount <= 1f)
                scroolbar.GetComponent().value = (float)(videoplayer.GetComponent().time * 30) / (float)videoplayer.GetComponent().frameCount;
        }else
        {
            Changetime = true;
        }
       // Debug.Log((float)(videoplayer.GetComponent().time * 30) / (float)videoplayer.GetComponent().frameCount);
        //Debug.Log(videoplayer.GetComponent().frameCount);//总时间
        if(Changetime)
        {
            videoplayer.GetComponent().time = scroolbar.GetComponent().value * (float)videoplayer.GetComponent().frameCount/30;
            Changetime = false;
        }
    }
}

像这样挂载即可

​​​​​​​unity内置播放器的进度条/音量/暂停功能集成_第1张图片

 

 暂停按钮代码:

在Assent文件夹新建一个Resources文件夹

里面放上stop和start的sprite来自动更换按钮ico

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

public class stop : MonoBehaviour
{
    public static bool Ispaused=false;
    public GameObject video;

    private void Start()
    {
    }
    public void clicked()
    {
       if(Ispaused)
        {
            Ispaused = false;
            StartCoroutine(waitingstart());
        }
        else
        {
            Ispaused = true;
            video.GetComponent().playbackSpeed = 0;
        }
    }
    private void FixedUpdate()
    {
        if(!Ispaused)
        this.GetComponent().sprite = Resources.Load("stop", typeof(Sprite)) as Sprite;
        else this.GetComponent().sprite = Resources.Load("start", typeof(Sprite)) as Sprite;
        if (Ispaused) video.GetComponent().playbackSpeed = 0;
    }
   IEnumerator waitingstart()//延迟一点点开始播放
    {
        yield return new WaitForSeconds(0.02f);
        video.GetComponent().playbackSpeed = 1;

    }
}

最终效果:视频https://www.bilibili.com/video/BV1jZ4y1d7w4?spm_id_from=333.999.0.0

你可能感兴趣的:(UNITY学习之路,unity,游戏引擎,c#)