Unity3d之Timeline功能开发

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);
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值