Unity TimeLine扩展

发表于2018-10-26
评论0 5.7k浏览
这篇内容给大家介绍下TimeLine的扩展使用,例如说想在TimeLine中右键添加自定义的Track,具体的步骤如下:

1、创建一个继承于TrackAsset的脚本
using UnityEngine;
using UnityEngine.Timeline;
[TrackColor(0.855f, 0.903f, 0.87f)]
// 添加的具体资源类型
[TrackClipType(typeof(NewPlayableAsset))]
// 绑定指定类型
//[TrackBindingType(typeof(GameObject))]
public class CustomTrack : TrackAsset {
}

2、创建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<GameObject> 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<NewPlayableBehaviour>.Create(graph, np);
        //return Playable.Create(graph);
    }
}

3、具体功能的实现
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);
    }
}

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引