【TimeLine】PlayableTrack的使用方法

版本unity2017.2.2+之后,TimeLine中添加的PlayableTrack只能是Playable Asset C# Script

TimeLine中的PlayableTrack可添加的clip类型

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

[System.Serializable]
public class NewPlayableAsset : PlayableAsset
{
    // ExposedReference类型使其变量可以选择Hirearchy面板中的对象
    // 在PlayableAsset中,仅仅使用public修饰的变量,只可以选择Project文件夹中的对象
    public ExposedReference playableDir;

    public ExposedReference ani;

    // 继承PlayableAsset后默认创建的工厂方法
    public override Playable CreatePlayable(PlayableGraph graph, GameObject go) {

        // 继承PlayableBehaviour C# Script的类对象
        NewPlayableBehaviour npb = new NewPlayableBehaviour();

        // graph.GetResolver():Returns the table used by the graph to resolve ExposedReferences.
        npb.playableDir =  playableDir.Resolve(graph.GetResolver());

        npb.ani = ani.Resolve(graph.GetResolver());

        // 自定以的返回结果需要使用ScriptPlayable来闯将对象
        return ScriptPlayable.Create(graph, npb);

        // 默认返回的是由graph创建的Playable对象
        //return Playable.Create(graph);
    }
}

PlayableTrack的具体调用实现,还需要另外一个脚本来实现,具体功能和调用时机如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
// A behaviour that is attached to a playable
public class NewPlayableBehaviour : PlayableBehaviour
{
    public PlayableDirector playableDir;
    public Animator ani;
    // Called when the owning graph starts playing
    public override void OnGraphStart(Playable playable) {

    }

    // Called when the owning graph stops playing
    public override void OnGraphStop(Playable playable) {

    }

    // Called when the state of the playable is set to Play
    public override void OnBehaviourPlay(Playable playable, FrameData info) {

    }

    // Called when the state of the playable is set to Paused
    public override void OnBehaviourPause(Playable playable, FrameData info) {

    }

    // Called each frame while the state is set to Play
    public override void PrepareFrame(Playable playable, FrameData info) {

        // 遍历playableDir中的TimeLine中所有的Track
        foreach (PlayableBinding item in playableDir.playableAsset.outputs)
        {
            // 所有track的名字
            //Debug.Log(item.streamName);
            if (item.streamName == "Animation Track1")
            {
                // 绑定一个对象,绑定到该Track
                playableDir.SetGenericBinding(item.sourceObject, playableDir.gameObject);


            }
        }
    }
}

你可能感兴趣的:(【Unity】,【效果】)