【Unity】TimeLine擴展

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

具體功能的實現在第三步

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

推薦閱讀:

TAG:Unity遊戲引擎 | 遊戲 |