【Unity】TimeLine扩展

若想在TimeLine中右键添加自定义的Track,那么第一步
创建一个继承于TrackAsset的脚本

using UnityEngine;
using UnityEngine.Timeline;
[TrackColor(0.855f, 0.903f, 0.87f)]
// 添加的具体资源类型
[TrackClipType(typeof(NewPlayableAsset))]
// 绑定指定类型
//[TrackBindingType(typeof(GameObject))]
public class CustomTrack : TrackAsset {
}

第二步,创建PlayableAsset类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
[System.Serializable]
public class NewPlayableAsset : PlayableAsset,ITimelineClipAsset
{
    public ExposedReference obj;
    // 使其暴露在面板中,但是该类需要序列化
    public NewPlayableBehaviour np = new NewPlayableBehaviour();

    public ClipCaps clipCaps
    {
        get { return ClipCaps.None; }
    }

    // Factory method that generates a playable based on this asset
    public override Playable CreatePlayable(PlayableGraph graph, GameObject go) {

        np.Obj = obj.Resolve(graph.GetResolver());
        return ScriptPlayable.Create(graph, np);
        //return Playable.Create(graph);
    }

}

具体功能的实现在第三步

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

// 如果不序列化,在面板中没办法看到参数接口
[Serializable]
public class NewPlayableBehaviour : PlayableBehaviour
{
    private GameObject obj;
    public string str;

    public GameObject Obj
    {
        get
        {
            return obj;
        }

        set
        {
            obj = value;
        }
    }

    // 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) {

        Debug.Log(Obj.name);
    }
}

你可能感兴趣的:(【Unity】,【Unity编辑器】)