包含此页的版本:
不含此页的版本:
此示例演示如何使用Pause()和Play()控制单个节点、分支或整个节点的播放状态的方法PlayableGraph.
设置节点的播放状态时,该状态将传播到其所有子项,而不管其播放状态如何。例如,如果子节点暂停,则将其父节点设置为 play 会将子节点设置为 play。
在此示例中,PlayableGraph有一个混合两个动画剪辑的混音器。一AnimationClipPlayable包装每个动画剪辑,并将Pause()方法显式暂停第二个可玩对象(clipPlayable1).因为第二个AnimationClipPlayable显式暂停,其内部时间不会前进,并且输出相同的值。此值是AnimationClipPlayable首先暂停了。
在使用ControlPlayState脚本,则项目必须具有以下内容:
RequireComponent属性 添加此组件(如果不存在)。要使用ControlPlayStatescript,请按照以下步骤作:
将脚本组件添加到游戏对象。为脚本文件命名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();
}
}
在 Script 组件中,选择动画剪辑 (clip0,clip1) 进行混合。
选择 运行(Play) 将编辑器切换到播放模式。
如果您已安装 PlayableGraph Visualizer 包,请选择ControlPlayState以显示 PlayableGraph。
ControlPlayState.脚本将暂停第二个动画剪辑。