Control the play state
Learn about the Pause() and Play() methods and how to control the play state of a PlayableGraph.
阅读时间2 分钟最后更新于 24 天前
This example demonstrates how to use the and methods to control the play state of a single node, a branch, or the entire .
Pause()Play()PlayableGraphWhen you set the play state of a node, the state propagates to all its children, regardless of their play state. For example, if a child node is paused, setting its parent node to play sets the child node to play.
In this example, the has a mixer that blends two animation clips. An wraps each animation clip and the method explicitly pauses the second playable (). Because the second is explicitly paused, its internal time doesn't advance and it outputs the same value. This value is the time when the was first paused.
PlayableGraphAnimationClipPlayablePause()clipPlayable1AnimationClipPlayableAnimationClipPlayablePrerequisites
Before you use the script in your project, your project must have the following:
ControlPlayState- A GameObject, such as a cube or a capsule. You don't need to manually add an Animator component to this GameObject. The attribute adds this component if it's not present.
RequireComponent - Two animation clips that animate the properties of the GameObject. For the blend between the animation clips to be noticeable, use two clips that are different from each other. For example, one clip animates the position of the GameObject while the other clip animates the rotation.
Add and run the script
To use the script in your project, follow these steps:
ControlPlayState-
Add a script component to your GameObject. Name the script fileand use the following code:
ControlPlayState.csusing 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(); }} -
In the Script component, select the animation clips (,
clip0) for the blend.clip1 -
Select Play to switch the Editor to Play mode.
-
If you have installed the PlayableGraph Visualizer package, selectto display the PlayableGraph.
ControlPlayState

The PlayableGraph generated by ControlPlayState. The script pauses the second animation clip.