Playable API - 简单动画混合2

角色在跑步时,同时播放向左或向右倾斜动画

using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;

public class SimpleAnimClipMix2 : MonoBehaviour
{
    public AnimationClip runLeftAnimClip;
    public AnimationClip runRightAnimClip;
    public AnimationClip runFrontAnimClip;
    [Range(-1, 1)]
    public float dirWeight;

    private PlayableGraph _graph;
    private AnimationMixerPlayable _animMixerPlayable;

    void Start()
    {
        _graph = PlayableGraph.Create("ChanPlayableGraph");
        var animationOutputPlayable = AnimationPlayableOutput.Create(_graph, "AnimationOutput", GetComponent<Animator>()); //往graph添加output

        _animMixerPlayable = AnimationMixerPlayable.Create(_graph, 3); //往graph添加playable(mixer)
        animationOutputPlayable.SetSourcePlayable(_animMixerPlayable);

        var runLeftAnimClipPlayable = AnimationClipPlayable.Create(_graph, runLeftAnimClip); //往graph添加playable(animClip)
        var runRightAnimClipPlayable = AnimationClipPlayable.Create(_graph, runRightAnimClip); //往graph添加playable(animClip)
        var runFrontAnimClipPlayable = AnimationClipPlayable.Create(_graph, runFrontAnimClip); //往graph添加playable(animClip)

        _graph.Connect(runLeftAnimClipPlayable, 0, _animMixerPlayable, 0);
        _graph.Connect(runRightAnimClipPlayable, 0, _animMixerPlayable, 1);
        _graph.Connect(runFrontAnimClipPlayable, 0, _animMixerPlayable, 2);

        _graph.Play();
    }

    void Update()
    {
        float leftWeight = dirWeight < 0 ? -dirWeight : 0;
        float rightWeight = dirWeight > 0 ? dirWeight : 0;
        float forwardWeight = 1 - leftWeight - rightWeight;

        _animMixerPlayable.SetInputWeight(0, leftWeight);
        _animMixerPlayable.SetInputWeight(1, rightWeight);
        _animMixerPlayable.SetInputWeight(2, forwardWeight);
    }

}

脚本:

运行效果:

PlayableGraph的可视化图形:

 

posted @ 2022-12-18 15:24  yanghui01  阅读(39)  评论(0编辑  收藏  举报