Play an animation clip
Learn how to create a PlayableGraph that plays a single animation clip.
읽는 시간 2분최근 업데이트: 한 달 전
This example demonstrates a with one playable output linked to one playable node. The playable node plays a single animation clip (). Before a playable node can use an animation clip, you must wrap the clip in an .
PlayableGraphclipAnimationClipPlayablePrerequisites
To use the script in your project, your project must have the following:
PlayAnimationClip- 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 - An animation clip that animates the properties of the GameObject. For example, a clip that animates the position and rotation of the GameObject.
Add and run the script
To use the script in your project, follow these steps:
PlayAnimationClip-
Add a script component to your GameObject. Name the script fileand use the following code:
PlayAnimationClip.csusing 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(); }} -
In the Script component, select the animation clip () that the PlayableGraph will play at runtime.
clip -
Select Play to switch the Editor to Play mode.
-
If you have installed the PlayableGraph Visualizer package, selectto display the PlayableGraph:
PlayAnimationClip

The PlayableGraph generated by the PlayAnimationClip script