一、普通进度条
1.创建进度条UI
2.写脚本控制进度条与Timeline
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;
using UnityEngine.UI;
public class SliderControl : MonoBehaviour
{
public PlayableDirector timeLinePlayer;
public Slider slider;
public bool isPlay;
public Text currentTime;
public Text durationTime;
public Sprite play;
public Sprite pause;
public Button playButton;
private void Start()
{
//var timelineAsset = Resources.Load<RecordingBase>("New Recording");
playButton.GetComponent<Button>().onClick.AddListener(StartPlay);
slider.maxValue= (float)timeLinePlayer.duration;
durationTime.text="/ "+ slider.maxValue.ToString("f2");
if (isPlay)
{
timeLinePlayer.Play();
}
else
{
timeLinePlayer.Stop();
}
}
private void Update()
{
if (isPlay)
{
slider.value = (float)timeLinePlayer.time;
}
currentTime.text = slider.value.ToString("f2");
}
//滑动条拖动时触发的函数
public void ValueChangeCheck()
{
timeLinePlayer.time = slider.value;
}
public void StartPlay()
{
if (!isPlay)
{
timeLinePlayer.Play();
playButton.GetComponent<Image>().sprite = pause;
isPlay = true;
}
else
{
timeLinePlayer.Pause();
playButton.GetComponent<Image>().sprite = play;
isPlay = false;
}
}
}
3.创建空物体添加脚本与组件
二、进度条优化
1.优化UI界面
2.编写脚本,新增倍速功能、暂停功能、退出功能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;
using UnityEngine.UI;
public class SliderControl : MonoBehaviour
{
public PlayableDirector timeLinePlayer;
public Slider slider;
public Text currentTime;
public Text durationTime;
public Text speedText;
public Text speedx2;
public Sprite play;
public Sprite pause;
public Button playButton;
public Button speedx2Button;
private float speed = 1.0f;
private bool isPlay;
private void Start()
{
//var timelineAsset = Resources.Load<RecordingBase>("New Recording");
playButton.GetComponent<Button>().onClick.AddListener(StartPlay);
speedx2Button.GetComponent<Button>().onClick.AddListener(ChangeSpeed);
speedx2Button.gameObject.SetActive(false);
slider.gameObject.SetActive(false);
}
private void Update()
{
if (isPlay)
{
slider.value = (float)timeLinePlayer.time;
}
currentTime.text = slider.value.ToString("00.00");
}
//滑动条拖动时触发的函数
public void ValueChangeCheck()
{
timeLinePlayer.time = slider.value;
}
/// <summary>
/// 开始暂停按钮
/// </summary>
public void StartPlay()
{
if (!isPlay)
{
timeLinePlayer.Play();
playButton.GetComponent<Image>().sprite = pause;
isPlay = true;
Time.timeScale = speed;
}
else
{
timeLinePlayer.Pause();
playButton.GetComponent<Image>().sprite = play;
isPlay = false;
Time.timeScale = 0f;
}
}
/// <summary>
/// 显示进度条并初始化
/// </summary>
public void ShowSlider()
{
slider.gameObject.SetActive(true);
slider.maxValue = (float)timeLinePlayer.duration;
durationTime.text = slider.maxValue.ToString("00.00");
speedText.text = "x1";
speedx2.text = "x2";
speed = 1.0f;
StartPlay();
}
/// <summary>
/// 结束并隐藏
/// </summary>
public void HiddenSlider()
{
isPlay = false;
timeLinePlayer.Stop();
slider.gameObject.SetActive(false);
Time.timeScale = 1.0f;
}
/// <summary>
/// 点击一次显示,再次点击隐藏
/// </summary>
public void ShowSpeed()
{
if (speedx2Button.gameObject.activeSelf)
{
speedx2Button.gameObject.SetActive(false);
}
else
{
speedx2Button.gameObject.SetActive(true);
}
}
public void ChangeSpeed()
{
if(speedText.text=="x1")
{
speedText.text = "x2";
speedx2.text = "x1";
speed = 2.0f;
}
else
{
speedText.text = "x1";
speedx2.text = "x2";
speed = 1.0f;
}
Time.timeScale = speed;
speedx2Button.gameObject.SetActive(false);
}
}
3.脚本赋值和绑定相关事件
4.关于进度条无法铺满问题,可以修改以下部分解决