一、Animator动画播放器
1.简单搭建UI,通过Slider来控制Cube当前状态动画的播放
2.给Cube加上Animator组件,做两个简单的动画(随便做个来回移动的动画就行),注意要关闭动画的循环播放
3.给摄像机挂在脚本,并拖拽对应组件
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SliderAni : MonoBehaviour
{
public static Animator ani;
public Slider ani_Slider;//滑动条
public Button play;//播放按钮
public Button pause;//暂停按钮
public Button replay;//重播按钮
public Button nextplay;//播放下一个动画按钮
public static bool isStop = true;
public List<AnimationClip> t = new List<AnimationClip>();//记录对应动画控制器中的所有动画片段
int index = 0;
void Start()
{
ani = GameObject.Find("Cube").GetComponent<Animator>();
AnimationClip[] clip = ani.runtimeAnimatorController.animationClips;
foreach(AnimationClip item in clip)
{
t.Add(item);
}
play.onClick.AddListener(PlayOnClickBtn);//添加监听事件
pause.onClick.AddListener(PauseOnClickBtn);
replay.onClick.AddListener(RePlayOnClickBtn);
nextplay.onClick.AddListener(NextPlayOnClickBtn);
ani_Slider.minValue = 0;
ani_Slider.maxValue = clip[0].length;
ani_Slider.value = 0;
ani.Play("run", 0, 0);
}
private void Update()
{
ChangeName();
}
public void ChangeName()
{
if (isStop)
{
if (index == 0)
{
ani_Slider.value = ani.GetCurrentAnimatorStateInfo(0).normalizedTime * t[index].length;
}
if (index == 1)
{
ani_Slider.value = ani.GetCurrentAnimatorStateInfo(0).normalizedTime * t[index].length;
}
}
else
{
ani.Play(t[index].name, 0, ani_Slider.value / t[index].length);
}
}
public void PlayOnClickBtn()
{
ani.speed = 1;
}
public void PauseOnClickBtn()
{
ani.speed = 0;
}
public void RePlayOnClickBtn()
{
ani.Play(t[index].name, 0, 0f);
ani.speed = 1;
}
public void NextPlayOnClickBtn()
{
index = 1;
ani_Slider.minValue = 0;
ani_Slider.maxValue = t[index].length;
ani_Slider.onValueChanged.RemoveAllListeners();
ani.Play("run2", 0, 0f);
}
}
4.给Slider挂载脚本
using UnityEngine;
using UnityEngine.EventSystems;
public class Test : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public void OnPointerDown(PointerEventData eventData)
{
SliderAni.ani.speed = 0;
SliderAni.isStop = false;
}
public void OnPointerUp(PointerEventData eventData)
{
SliderAni.ani.speed = 1;
SliderAni.isStop = true;
}
}
5.运行Unity即可文章来源地址https://www.toymoban.com/news/detail-724653.html
二、Video Player视频播放器
1.搭建场景
2.RawImage游戏对象的Raw Image组件和Video Player组件添加同一个Render Texture(可在Project中创建),给Video Player赋值视频
3.在Dropdown游戏对象上设置视频倍数
4.将脚本挂载到Slider游戏对象上,并赋值对应参数
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using UnityEngine.EventSystems;
public class VideoPlayerController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public VideoPlayer videoPlayer;
public Slider progressBar;
public Dropdown speedDropdown;
public Button playBt;
public Button pauseBt;
private bool isDragging;
void Start()
{
videoPlayer.loopPointReached += OnVideoFinished;
playBt.onClick.AddListener(PlayVideo);
pauseBt.onClick.AddListener(PauseVideo);
speedDropdown.onValueChanged.AddListener(ChangePlaybackSpeed);
}
void Update()
{
if (!isDragging && videoPlayer.isPlaying && progressBar.value < 1f)
{
// 更新进度条的值
progressBar.value = (float)(videoPlayer.time / videoPlayer.clip.length);
}
}
private void PlayVideo()
{
videoPlayer.Play();
}
private void PauseVideo()
{
videoPlayer.Pause();
}
//拖动滑动条后暂停播放视频再改变视频的播放时间,当行为被触发时执行一次
public void OnPointerDown(PointerEventData eventData)
{
isDragging = true;
float progress = progressBar.value;
videoPlayer.time = progress * videoPlayer.clip.length;
progressBar.value = (float)(videoPlayer.time / videoPlayer.clip.length);//更新进度条的值
}
public void OnPointerUp(PointerEventData eventData)
{
isDragging = false;
videoPlayer.Play();
}
//当行为被触发时会持续执行
public void OnDrag(PointerEventData eventData)
{
// 拖动进度条改变视频播放进度
float progress = progressBar.value;
videoPlayer.time = progress * videoPlayer.clip.length;
}
public void OnVideoFinished(VideoPlayer vp)
{
// 视频播放完成,可根据需要进行操作
}
public void ChangePlaybackSpeed(int value)
{
// 获取选择的倍速值
string selectedSpeedString = speedDropdown.transform.GetChild(0).GetComponent<Text>().text;
// 解析倍速值
float selectedSpeed;
if (float.TryParse(selectedSpeedString, out selectedSpeed))
{
// 设置播放速度
videoPlayer.playbackSpeed = selectedSpeed;
print(videoPlayer.playbackSpeed);
}
}
}
文章来源:https://www.toymoban.com/news/detail-724653.html
5.运行Unity即可
到了这里,关于Unity中两种播放器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!