栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

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

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

像这样挂载即可

​​​​​​​

 

 暂停按钮代码:

在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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/906262.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号