Version: 6000.3
语言: 中文
PlayableGraph 可视化工具
混合两个动画剪辑

播放动画剪辑

此示例演示了PlayableGraph一个可播放输出链接到一个可播放节点。可播放节点播放单个动画剪辑(clip).在可播放节点可以使用动画剪辑之前,必须将剪辑包装在AnimationClipPlayable.

先决条件

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

  • 一个游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
    请参阅术语表
    例如立方体或胶囊。您无需手动添加Animator 组件模型上的一个组件,使用动画系统为该模型设置动画。该组件具有对控制动画的 Animator 控制器资源的引用。更多信息
    请参阅术语表
    到这个游戏对象。这RequireComponent属性 添加此组件(如果不存在)。
  • 对游戏对象的属性进行动画处理的动画剪辑。例如,对游戏对象的位置和旋转进行动画处理的剪辑。

添加并运行脚本

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

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

    using UnityEngine;
    using UnityEngine.Playables;
    using UnityEngine.Animations;
    
    [RequireComponent(typeof(Animator))]
    public class PlayAnimationClip : MonoBehaviour
    {
        public AnimationClip clip;
        PlayableGraph graph;
    
        void Start()
        {
            graph = PlayableGraph.Create("PlayAnimationClip");
            graph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
    
            var output = AnimationPlayableOutput.Create(graph, "Animation", GetComponent<Animator>());
    
            // Wrap the clip in a playable.
            var clipPlayable = AnimationClipPlayable.Create(graph, clip);
    
            // Connect the Playable to an output.
            output.SetSourcePlayable(clipPlayable);
    
            // Plays the Graph.
            graph.Play();
        }
    
        void OnDisable()
        {
            // Destroys all Playables and PlayableOutputs created by the graph.
            graph.Destroy();
        }
    }
    
  2. 在 Script 组件中,选择动画剪辑 (clip),PlayableGraph 将在运行时播放。

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

  4. 如果您已安装 PlayableGraph Visualizer 包,请选择PlayAnimationClip以显示 PlayableGraph:

PlayAnimationClip脚本生成的PlayableGraph
PlayAnimationClip脚本

其他资源

PlayableGraph 可视化工具
混合两个动画剪辑