using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;
using UnityEngine.Events;
/// <summary>
/// TimeLine控制器
/// </summary>
public class TimeLineController : MonoBehaviour
{
public PlayableDirector playableDirector;
public UnityEvent OnPlayActiveEvent = new UnityEvent();
public UnityEvent OnPauseActiveEvent = new UnityEvent();
public UnityEvent OnStopActiveEvent = new UnityEvent();
private void Start()
{
playableDirector.playOnAwake = false;
playableDirector.Stop();
playableDirector.played += OnPlayablePlayed;
playableDirector.paused += OnPlayablePaused;
playableDirector.stopped += OnPlayableStopped;
}
private void OnPlayablePlayed(PlayableDirector obj)
{
if (OnPlayActiveEvent != null)
{
OnPlayActiveEvent.Invoke();
}
Debug.Log("play!" + obj.name);
}
private void OnPlayablePaused(PlayableDirector obj)
{
if (OnPauseActiveEvent != null)
{
OnPauseActiveEvent.Invoke();
}
Debug.Log("pause!" + obj.name);
}
private void OnPlayableStopped(PlayableDirector obj)
{
if (OnStopActiveEvent != null)
{
OnStopActiveEvent.Invoke();
}
Debug.Log("stop!" + obj.name);
}
public void Update()
{
#if UNITY_EDITOR
if (Input.GetKeyDown(KeyCode.G))
{
OnStart();
}
if (Input.GetKeyDown(KeyCode.H))
{
OnPause();
}
if (Input.GetKeyDown(KeyCode.K))
{
OnResume();
}
if (Input.GetKeyDown(KeyCode.T))
{
OnStop();
}
if (Input.GetKeyDown(KeyCode.A))
{
OnTimeLinePause();
StartCoroutine(OnTimeLineRewind());
}
if (Input.GetKeyDown(KeyCode.B))
{
OnTimeLineResume();
}
#endif
}
/// <summary>
/// 启动
/// </summary>
public void OnStart()
{
playableDirector.Play();
}
public void OnPause()
{
playableDirector.Pause();
}
public void OnResume()
{
playableDirector.Resume();
}
public void OnStop()
{
playableDirector.Stop();
}
/// <summary>
/// 倒播
/// </summary>
/// <returns></returns>
public IEnumerator OnTimeLineRewind()
{
yield return new WaitForSeconds(0.001f*Time.deltaTime);
playableDirector.time -= 1.0f * Time.deltaTime;
playableDirector.Evaluate();
if (playableDirector.time < 0f)
{
playableDirector.time = 0f;
playableDirector.Evaluate();
}
else
{
StartCoroutine(OnTimeLineRewind());
}
}
/// <summary>
/// 暂停
/// </summary>
public void OnTimeLinePause()
{
if (playableDirector != null)
{
playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(0);
}
}
/// <summary>
/// 恢复播放
/// </summary>
public void OnTimeLineResume()
{
if (playableDirector != null)
{
playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(1);
}
}
}
1067

被折叠的 条评论
为什么被折叠?



