This example demonstrates a PlayableGraph with one playable output linked to one playable node. The playable node plays a single animation clip (clip). Before a playable node can use an animation clip, you must wrap the clip in an AnimationClipPlayable.
To use the PlayAnimationClip script in your project, your project must have the following:
RequireComponent attribute adds this component if it’s not present.To use the PlayAnimationClip script in your project, follow these steps:
Add a script component to your GameObject. Name the script file PlayAnimationClip.cs and use the following code:
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();
}
}
In the Script component, select the animation clip (clip) that the PlayableGraph will play at runtime.
Select Play to switch the Editor to Play mode.
If you have installed the PlayableGraph Visualizer package, select PlayAnimationClip to display the PlayableGraph:
PlayAnimationClip script