Version: 6000.3
语言: 中文
将动画剪辑与控制器混合
控制可播放片段的时序

控制播放状态

此示例演示如何使用Pause()Play()控制单个节点、分支或整个节点的播放状态的方法PlayableGraph.

设置节点的播放状态时,该状态将传播到其所有子项,而不管其播放状态如何。例如,如果子节点暂停,则将其父节点设置为 play 会将子节点设置为 play。

在此示例中,PlayableGraph有一个混合两个动画剪辑的混音器。一AnimationClipPlayable包装每个动画剪辑,并将Pause()方法显式暂停第二个可玩对象(clipPlayable1).因为第二个AnimationClipPlayable显式暂停,其内部时间不会前进,并且输出相同的值。此值是AnimationClipPlayable首先暂停了。

先决条件

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

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

添加并运行脚本

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

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

    using UnityEngine;
    using UnityEngine.Playables;
    using UnityEngine.Animations;
    
    [RequireComponent(typeof(Animator))]
    public class ControlPlayState : MonoBehaviour
    {
        public AnimationClip clip0;
        public AnimationClip clip1;
    
        PlayableGraph graph;
        AnimationMixerPlayable mixer;
    
        void Start()
        {
            // Create the graph and the mixer, then bind them to the Animator.
            graph = PlayableGraph.Create("ControlPlayState");
            mixer = AnimationMixerPlayable.Create(graph, 2);
    
            var output = AnimationPlayableOutput.Create(graph, "Animation", GetComponent<Animator>());
            output.SetSourcePlayable(mixer);
    
            // Create AnimationClipPlayable playables, then connect them to the mixer.
            var clipPlayable0 = AnimationClipPlayable.Create(graph, clip0);
            var clipPlayable1 = AnimationClipPlayable.Create(graph, clip1);
            graph.Connect(clipPlayable0, 0, mixer, 0);
            graph.Connect(clipPlayable1, 0, mixer, 1);
    
            // Adjust the weight of each mixer input.
            mixer.SetInputWeight(0, 0.5f);
            mixer.SetInputWeight(1, 0.5f);
    
            // Pause the second clip playable.
            clipPlayable1.Pause();
    
            // Play the Graph.
            graph.Play();
        }
    
        void OnDisable()
        {
            // Destroy all Playables and outputs created by the graph.
            graph.Destroy();
        }
    }
    
  2. 在 Script 组件中,选择动画剪辑 (clip0,clip1) 进行混合。

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

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

ControlPlayState 生成的 PlayableGraph。脚本将暂停第二个动画剪辑。
ControlPlayState.脚本将暂停第二个动画剪辑。

其他资源

将动画剪辑与控制器混合
控制可播放片段的时序