• 欢迎光临~

Timeline - 自定义轨道

开发技术 开发技术 2022-12-27 次浏览

Playable轨道和自定义轨道的区别

1) Playable轨道中,自定义PlayableBehaviour的上一级只能是Playable。

Timeline - 自定义轨道

自定义轨道中,上一级是我们自定义的Playable

Timeline - 自定义轨道

2) 自定义轨道可以直接在轨道上绑定目标,Playable轨道中则不行,需要只能使用ExposedReference<CanvasGroup> _target的方式

Timeline - 自定义轨道

 

自定义轨道来实现CanvasGroup补间动画

自定义轨道类

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

自定义轨道需要有一个Mixer的Playable

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

轨道资源类(同Playable轨道)

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

}

轨道资源行为类

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;

}

#

Timeline - 自定义轨道

PlayableGraph的可视化图形:

Timeline - 自定义轨道

 

其他

1) 如果我们注释掉TrackBindingType,会发现无法在轨道上直接绑定对象了

Timeline - 自定义轨道

 

程序员灯塔
转载请注明原文链接:Timeline - 自定义轨道
喜欢 (0)