先创建两个滚动条,分别控制进度条和音量
创建一个按钮,控制播放与暂停
给视频播放器挂这个代码:
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<CanvasGroup>().alpha = 1;
stoptime = 50;//多久后开始变透明
}
if (Input.GetMouseButtonUp(0))
{
currPos = Input.mousePosition;
}
//Debug.Log(videoplayer.GetComponent<VideoPlayer>().time);//现在时间
videoplayer.GetComponent<VideoPlayer>().SetDirectAudioVolume(0,volumebar.GetComponent<Scrollbar>().value);
}
private void FixedUpdate()
{
stoptime--;
if (stop.Ispaused == false&&stoptime<=0)
{
Stopbtn.GetComponent<CanvasGroup>().alpha -= 0.04f;
}
if (!Input.GetMouseButton(0))
{
Debug.Log("click");
if ((float)(videoplayer.GetComponent<VideoPlayer>().time * 30) / (float)videoplayer.GetComponent<VideoPlayer>().frameCount >= 0f && (float)(videoplayer.GetComponent<VideoPlayer>().time * 30) / (float)videoplayer.GetComponent<VideoPlayer>().frameCount <= 1f)
scroolbar.GetComponent<Scrollbar>().value = (float)(videoplayer.GetComponent<VideoPlayer>().time * 30) / (float)videoplayer.GetComponent<VideoPlayer>().frameCount;
}else
{
Changetime = true;
}
// Debug.Log((float)(videoplayer.GetComponent<VideoPlayer>().time * 30) / (float)videoplayer.GetComponent<VideoPlayer>().frameCount);
//Debug.Log(videoplayer.GetComponent<VideoPlayer>().frameCount);//总时间
if(Changetime)
{
videoplayer.GetComponent<VideoPlayer>().time = scroolbar.GetComponent<Scrollbar>().value * (float)videoplayer.GetComponent<VideoPlayer>().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<VideoPlayer>().playbackSpeed = 0;
}
}
private void FixedUpdate()
{
if(!Ispaused)
this.GetComponent<Image>().sprite = Resources.Load("stop", typeof(Sprite)) as Sprite;
else this.GetComponent<Image>().sprite = Resources.Load("start", typeof(Sprite)) as Sprite;
if (Ispaused) video.GetComponent<VideoPlayer>().playbackSpeed = 0;
}
IEnumerator waitingstart()//延迟一点点开始播放
{
yield return new WaitForSeconds(0.02f);
video.GetComponent<VideoPlayer>().playbackSpeed = 1;
}
}
最终效果:视频https://www.bilibili.com/video/BV1jZ4y1d7w4?spm_id_from=333.999.0.0
|