Timeline - 自定义轨道
Playable轨道和自定义轨道的区别
1) Playable轨道中,自定义PlayableBehaviour的上一级只能是Playable。
自定义轨道中,上一级是我们自定义的Playable
2) 自定义轨道可以直接在轨道上绑定目标,Playable轨道中则不行,需要只能使用ExposedReference<CanvasGroup> _target的方式
自定义轨道四元素
Track——单行轨道,使用TrackAsset来保存数据
Mixer——混合器,根据inputWeight对片段进行混合
Clip——轨道上的片段,使用PlayableAsset来保存相关数据并关联片段逻辑
Behaviour——片段逻辑
自定义轨道来实现CanvasGroup补间动画
1) 轨道数据
using UnityEngine; using UnityEngine.Playables; using UnityEngine.Timeline; [TrackClipType(typeof(CanvasGroupClip))] [TrackBindingType(typeof(CanvasGroup))] //用于直接在轨道上绑定对象 public class MyCanvasGroupTrack : TrackAsset { public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount) { return ScriptPlayable<CanvasGroupMixerBehaviour>.Create(graph, inputCount); } }
2) 自定义Mixer
using UnityEngine; using UnityEngine.Playables; public class CanvasGroupMixerBehaviour : PlayableBehaviour {
public override void ProcessFrame(Playable playable, FrameData info, object playerData) { if (!(playerData is CanvasGroup canvasGroup)) return; float blendedValue = 0; float totalWeight = 0; var inputCount = playable.GetInputCount(); for (var i = 0; i < inputCount; ++i) { var playableInput = (ScriptPlayable<CanvasGroupBehaviour>)playable.GetInput(i); var behaviour = playableInput.GetBehaviour(); var inputWeight = playable.GetInputWeight(i); float progress = (float)(playableInput.GetTime() / playableInput.GetDuration()); if (inputWeight == 0) continue; blendedValue += Mathf.Lerp(behaviour.startValue, behaviour.endValue, progress) * inputWeight; totalWeight += inputWeight; } //blendedValue += canvasGroup.alpha * (1 - totalWeight); canvasGroup.alpha = blendedValue; Debug.Log($"CanvasGroupMixerBehaviour: count: {inputCount}, alpha:{blendedValue}"); } }
3) 轨道片段数据
using UnityEngine; using UnityEngine.Playables; [System.Serializable] public class CanvasGroupClip : PlayableAsset { public CanvasGroupBehaviour template = new CanvasGroupBehaviour(); public override Playable CreatePlayable(PlayableGraph graph, GameObject go) { var playable = ScriptPlayable<CanvasGroupBehaviour>.Create(graph, template); return playable; } }
4) 轨道片段逻辑
using UnityEngine; using UnityEngine.Playables; [System.Serializable] public class CanvasGroupBehaviour : PlayableBehaviour { [SerializeField] public float _startValue; [SerializeField] public float _endValue; public float startValue => _startValue; public float endValue => _endValue; }
#
PlayableGraph的可视化图形:
其他
1) 如果我们注释掉TrackBindingType,会发现无法在轨道上直接绑定对象了
参考
Unity TimeLine学习笔记_such_so的博客-CSDN博客_timeline快捷键
分类:
Unity
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2021-12-27 shader Keyword无法关闭问题