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快捷键

 

posted @   yanghui01  阅读(529)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
历史上的今天:
2021-12-27 shader Keyword无法关闭问题
点击右上角即可分享
微信分享提示