Version: 6000.3
语言: 中文
混合两个动画剪辑
控制播放状态

将动画剪辑与控制器混合

此示例演示如何使用AnimationMixerPlayableAnimationClip使用AnimatorController.为此,你必须首先将每个资产包装在其相应的可玩对象中:

  • AnimationClip (clip) 在AnimationClipPlayable (clipPlayable).
  • RuntimeAnimatorController (controller) 在AnimatorControllerPlayable (controllerPlayable).

SetInputWeight()方法动态调整每个可玩对象的混合权重。

先决条件

要使用BlendClipWithController脚本,则项目必须具有以下内容:

  • 一个游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
    请参阅术语表
    例如立方体或胶囊。您无需手动添加Animator 组件模型上的一个组件,使用动画系统为该模型设置动画。该组件具有对控制动画的 Animator 控制器资源的引用。更多信息
    请参阅术语表
    到这个游戏对象。这RequireComponent属性 添加此组件(如果不存在)。
  • 动画剪辑可用于动画角色或简单动画的动画数据。它是一个简单的“单位”运动片段,例如(一个特定实例)“Idle”、“Walk”或“Run”。更多信息
    请参阅术语表
    对游戏对象的属性进行动画处理。例如,更改游戏对象的位置和旋转的动画。
  • 具有至少一个状态的动画器控制器,该状态使用与对游戏对象进行动画处理的动画剪辑不同的动画剪辑。如果使用相同的动画剪辑,则当 PlayableGraph 在剪辑和控制器之间混合时,没有区别。

添加并运行脚本

要使用BlendClipWithControllerscript,请按照以下步骤作:

  1. 将脚本组件添加到游戏对象。为脚本文件命名BlendClipWithController.cs并使用以下代码:

    using UnityEngine;
    using UnityEngine.Playables;
    using UnityEngine.Animations;
    
    [RequireComponent(typeof(Animator))]
    public class BlendClipWithController : MonoBehaviour
    {
        public AnimationClip clip;
        public RuntimeAnimatorController controller;
        public float weight;
    
        PlayableGraph graph;
        AnimationMixerPlayable mixer;
    
        void Start()
        {
            // Create the graph, the mixer, and bind them to the Animator.
            graph = PlayableGraph.Create("BlendClipWithController");
            mixer = AnimationMixerPlayable.Create(graph, 2);
    
            var output = AnimationPlayableOutput.Create(graph, "Animation", GetComponent<Animator>());
            output.SetSourcePlayable(mixer);
    
            // Create playables and connect them to the mixer.
            var clipPlayable = AnimationClipPlayable.Create(graph, clip);
            var controllerPlayable = AnimatorControllerPlayable.Create(graph, controller);
            graph.Connect(clipPlayable, 0, mixer, 0);
            graph.Connect(controllerPlayable, 0, mixer, 1);
    
            // Play the Graph.
            graph.Play();
        }
    
        void Update()
        {
            // Clamp the weight between 0 and 1.
            weight = Mathf.Clamp01(weight);
                
            // Adjust the weight of each mixer input.
            mixer.SetInputWeight(0, 1.0f-weight);
            mixer.SetInputWeight(1, weight);
        }
    
        void OnDisable()
        {
            // Destroy all Playables and Outputs created by the graph.
            graph.Destroy();
        }
    }
    
  2. 在 脚本(Script) 组件中,为输入节点 0 选择动画剪辑,为输入节点 1 选择 Animator Controller。

  3. 指定 Animator 控制器相对于动画剪辑的权重。
    该脚本相对于输入节点 1 调整输入节点 0 的权重,使组合权重等于 1.0。例如,权重为 0.4 时,将 Animator 控制器设置为 40% 权重,将动画剪辑设置为 60% 权重。

  4. 选择 运行(Play) 将编辑器切换到播放模式。

  5. 在 脚本 组件中,尝试不同的权重。

  6. 如果您已安装 PlayableGraph Visualizer 包,请选择BlendClipWithController以显示 PlayableGraph。

由 BlendClipWithController 脚本生成的 PlayableGraph
BlendClipWithController脚本

其他资源

混合两个动画剪辑
控制播放状态