赞
踩
一直想做一个切换进度的功能,因为MovieTextureAPI过少,一直有这个遗憾,unity5.6之后加了一个videoPlayer组件,就想做一个,由于接触unity时间也不长,也踩了很多坑,不管怎么说,还是做了出来。不多说,上代码:
- using UnityEngine;
- using UnityEngine.Video;
-
- public static class VideoController
- {
- /// <summary>
- /// 获取视频总时长
- /// </summary>
- /// <param name="vsp"></param>
- /// <returns></returns>
- public static int GetVideoTimeCount(this VideoPlayer vp)
- {
- return (int)(vp.frameCount / vp.frameRate);
- }
- /// <summary>
- /// 获取视频进度
- /// </summary>
- /// <param name="vsp"></param>
- /// <returns></returns>
- public static float GetVideoProgression(this VideoPlayer vp)
- {
- return (float)((vp.time * vp.frameRate)/(vp.frameCount / vp.frameRate));
- }
-
- /// <summary>
- /// 设置视频进度
- /// </summary>
- /// <param name="vp"></param>
- /// <param name="progression"></param>
- public static void SetVideoProgression(this VideoPlayer vp, float progression)
- {
- float time = (int)vp.frameCount / vp.frameRate * progression;
- vp.time = time;
- vp.Play();
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Video;
- public class VideoTest : MonoBehaviour
- {
- public VideoPlayer vp;
- public Slider progression;
- public Text timeCount;
- public Text currentTime;
- void Start()
- {
- vp.Play();
- progression.value = vp.GetVideoProgression();
- progression.onValueChanged.AddListener(Changed);
- DateFormat((int)vp.GetVideoTimeCount(), timeCount);
- }
-
-
- private void DateFormat(int sec, Text text)
- {
- TimeSpan span = new TimeSpan(0, 0, 0, sec);
- text.text = (int)span.Hours + ":" + (int)span.Minutes + ":" + (int)span.Seconds;
- }
-
- // Update is called once per frame
- void Update()
- {
- DateFormat((int)vp.time, currentTime);
- }
- private void Changed(float value)
- {
- vp.SetVideoProgression(value);
- }
- public void Play()
- {
- vp.Play();
- }
- public void Pause()
- {
- vp.Pause();
- }
- }
效果图如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。