包含此页的版本:
不含此页的版本:
此示例演示如何使用AnimationMixerPlayable将AnimationClip使用AnimatorController.为此,你必须首先将每个资产包装在其相应的可玩对象中:
AnimationClip (clip) 在AnimationClipPlayable (clipPlayable).RuntimeAnimatorController (controller) 在AnimatorControllerPlayable (controllerPlayable).这SetInputWeight()方法动态调整每个可玩对象的混合权重。
要使用BlendClipWithController脚本,则项目必须具有以下内容:
RequireComponent属性 添加此组件(如果不存在)。要使用BlendClipWithControllerscript,请按照以下步骤作:
将脚本组件添加到游戏对象。为脚本文件命名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();
}
}
在 脚本(Script) 组件中,为输入节点 0 选择动画剪辑,为输入节点 1 选择 Animator Controller。
指定 Animator 控制器相对于动画剪辑的权重。
该脚本相对于输入节点 1 调整输入节点 0 的权重,使组合权重等于 1.0。例如,权重为 0.4 时,将 Animator 控制器设置为 40% 权重,将动画剪辑设置为 60% 权重。
选择 运行(Play) 将编辑器切换到播放模式。
在 脚本 组件中,尝试不同的权重。
如果您已安装 PlayableGraph Visualizer 包,请选择BlendClipWithController以显示 PlayableGraph。
BlendClipWithController脚本