/
Playables API
The Playables API provides a way to create tools, effects or other gameplay mechanisms by organizing and evaluating data sources in a tree-like structure known as the PlayableGraph. The PlayableGraph allows you to mix, blend, and modify multiple data sources, and play them through a single output.
The Playables API supports animation, audio and scripts
. The Playables API also provides the capacity to interact with the animation system and audio system through scripting.
Although the Playables API is currently limited to animation, audio, and scripts, it is a generic API that will eventually be used by video and other systems.
Playable vs Animation
The animation system already has a graph editing tool, it’s a state machine
system that is restricted to playing animation. The Playables API is designed to be more flexible and to support other systems. The Playables API also allows for the creation of graphs not possible with the state machine. These graphs represent a flow of data, indicating what each node produces and consumes. In addition, a single graph is not limited to a single system. A single graph may contain nodes for animation, audio, and scripts.
Advantages of using the Playables API
-
The Playables API allows for dynamic animation blending. This means that objects in the scenes
could provide their own animations. For example, animations for weapons, chests, and traps could be dynamically added to the PlayableGraph and used for a certain duration. -
The Playables API allows you to easily play a single animation without the overhead involved in creating and managing an AnimatorController asset.
-
The Playables API allows users to dynamically create blending graphs and control the blending weights directly frame by frame.
-
A PlayableGraph can be created at runtime, adding playable node as needed, based on conditions. Instead of having a huge “one-size-fit-all” graph where nodes are enabled and disabled, the PlayableGraph can be tailored to fit the requirements of the current situation.
The PlayableGraph
The PlayableGraph defines a set of playable outputs that are bound to a GameObject
or component
. The PlayableGraph also defines a set of playables
and their relationships. Figure 1 provides an example.
The PlayableGraph is responsible for the life cycle of its playables and their outputs. Use the PlayableGraph to create, connect, and destroy playables.
Figure 1: A sample PlayableGraph
In Figure 1, when displaying a PlayableGraph, the term “Playable” is removed from the names of graph nodes to make it more compact. For example, the node named “AnimationClipPlayable” is shown as “AnimationClip.”
A playable is a C# struct that implements the IPlayable interface. It is used to define its relationship with other playables. Likewise, a playable output is a C# struct that implements IPlayableOutput and is used to define the output of a PlayableGraph.
Figure 2 shows the most common core playable types. Figure 3 shows the core playable output types.
Figure 2: Core playable types
Figure 3: Core playable output types
The playable core types and playable output types are implemented as C# structs to avoid allocating memory for garbage collection.
‘Playable’ is the base type for all playables, meaning that you can always implicitly cast a playable to it. The opposite is not true, and an exception will be thrown if a ‘Playable’ is explicitly casted into an incompatible type. It also defines all the basic methods that can be executed on a playable. To access type-specific methods, you need to cast our playable to the appropriate type.
The same thing is true for ‘PlayableOutput’, it is the base type for all playable outputs and it defines the basic methods.
Note: Playable
and PlayableOutput
do not expose a lot of methods. Instead, the ‘PlayableExtensions’ and ‘PlayableOutputExtensions’ static classes provide extension methods.
All non-abstract playables have a public static method Create()
that creates a playable of the corresponding type. The ‘Create()’ method always takes a PlayableGraph as its first parameter, and that graph owns the newly created playable. Additional parameters may be required for some type of playables. Non-abstract playable outputs also expose a Create()
method.
A valid playable output should be linked to a playable. If a playable output is not linked to a playable, the playable output does nothing. To link a playable output to a playable, use the PlayableOutput.SetSourcePlayable()
method. The linked playable acts as the root of the playable tree, for that specific playable output.
To connect two playables together, use the PlayableGraph.Connect()
method. Note that some playables cannot have inputs.
Use the PlayableGraph.Create()
static method to create a PlayableGraph.
Play a PlayableGraph with the PlayableGraph.Play()
method.
Stop a playing PlayableGraph with thePlayableGraph.Stop()
method.
Evaluate the state of a PlayableGraph, at a specific time, with the PlayableGraph.Evaluate()
method.
Destroy a PlayableGraph manually with the PlayableGraph.Destroy()
method. This method automatically destroys all playables and playable outputs that were created by the PlayableGraph. You must manually call this destroy method to destroy a PlayableGraph, otherwise Unity issues an error message.
ScriptPlayable and PlayableBehaviour
To create your own custom playable, it must be inherited from the PlayableBehaviour base class. public class MyCustomPlayableBehaviour : PlayableBehaviour { // Implementation of the custom playable behaviour // Override PlayableBehaviour methods as needed }
To use a PlayableBehaviour as a custom playable, it also must be encapsulated within a ScriptPlayable<> object. If you don’t have an instance of your custom playable, you can create a ScriptPlayable<> for your object by calling:
ScriptPlayable<MyCustomPlayableBehaviour>.Create(playableGraph);
If you already have an instance of your custom playable, you can wrap it with a ScriptPlayable<> by calling:
MyCustomPlayableBehaviour myPlayable = new MyCustomPlayableBehaviour();
ScriptPlayable<MyCustomPlayableBehaviour>.Create(playableGraph, myPlayable);
In this case, the instance is cloned before it is assigned to the ScriptPlayable<>. As it is, this code does exactly the same as the previous code; the difference is that myPlayable
can be a public property that would be configured in the inspector, and you can then set up your behaviour for each instance of your script.
You can get the PlayableBehaviour object from the ScriptPlayable<> by using the ScriptPlayable<T> .GetBehaviour()
method.
Playables Examples
PlayableGraph Visualizer
All of the examples in this document use the PlayableGraph Visualizer (Pictured below) to illustrate the trees and nodes created by the Playables
API. The Playable graph
Visualizer is a tool available through GitHub.
To use the PlayableGraph Visualizer:
-
Download the PlayableGraph Visualizer that corresponds with your version of Unity from the GitHub repository
-
Open the tool by selecting Window > PlayableGraph Visualizer
-
Register your graph using GraphVisualizerClient.Show(PlayableGraph graph, string name).
The GraphVisualizer window
Playables in the graph are represented by colored nodes. Wire color intensity indicates the weight of the blending. See GitHub for more information on this tool.
Playing a single animation clip on a GameObject
This example demonstrates a simple PlayableGraph
with a single playable output that is linked to a single playable node. The playable node plays a single animation clip
(clip). An AnimationClipPlayable
must wrap the animation clip to make it compatible with the Playables API.
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
[RequireComponent(typeof(Animator))]
public class PlayAnimationSample : MonoBehaviour
{
public AnimationClip clip;
PlayableGraph playableGraph;
void Start()
{
playableGraph = PlayableGraph.Create();
playableGraph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
var playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
// Wrap the clip in a playable
var clipPlayable = AnimationClipPlayable.Create(playableGraph, clip);
// Connect the Playable to an output
playableOutput.SetSourcePlayable(clipPlayable);
// Plays the Graph.
playableGraph.Play();
}
void OnDisable()
{
// Destroys all Playables and PlayableOutputs created by the graph.
playableGraph.Destroy();
}
}
The PlayableGraph generated by PlayAnimationSample
Use AnimationPlayableUtilities
to simplify the creation and playback of animation playables, as shown in the following example:__ __
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
[RequireComponent(typeof(Animator))]
public class PlayAnimationUtilitiesSample : MonoBehaviour
{
public AnimationClip clip;
PlayableGraph playableGraph;
void Start()
{
AnimationPlayableUtilities.PlayClip(GetComponent<Animator>(), clip, out playableGraph);
}
void OnDisable()
{
// Destroys all Playables and Outputs created by the graph.
playableGraph.Destroy();
}
}
Creating an animation blend tree
This example demonstrates how to use the AnimationMixerPlayable
to blend two animation clips. Before blending the animation clips, they must be wrapped by playables. To do this, an AnimationClipPlayable
(clipPlayable0 and clipPlayable1) wraps each AnimationClip
(clip0 and clip1). The SetInputWeight()
method dynamically adjusts the blend weight of each playable.
Although not shown in this example, you can also use AnimationMixerPlayable
to blend playable mixers and other playables.
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
[RequireComponent(typeof(Animator))]
public class MixAnimationSample : MonoBehaviour
{
public AnimationClip clip0;
public AnimationClip clip1;
public float weight;
PlayableGraph playableGraph;
AnimationMixerPlayable mixerPlayable;
void Start()
{
// Creates the graph, the mixer and binds them to the Animator.
playableGraph = PlayableGraph.Create();
var playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
mixerPlayable = AnimationMixerPlayable.Create(playableGraph, 2);
playableOutput.SetSourcePlayable(mixerPlayable);
// Creates AnimationClipPlayable and connects them to the mixer.
var clipPlayable0 = AnimationClipPlayable.Create(playableGraph, clip0);
var clipPlayable1 = AnimationClipPlayable.Create(playableGraph, clip1);
playableGraph.Connect(clipPlayable0, 0, mixerPlayable, 0);
playableGraph.Connect(clipPlayable1, 0, mixerPlayable, 1);
// Plays the Graph.
playableGraph.Play();
}
void Update()
{
weight = Mathf.Clamp01(weight);
mixerPlayable.SetInputWeight(0, 1.0f-weight);
mixerPlayable.SetInputWeight(1, weight);
}
void OnDisable()
{
// Destroys all Playables and Outputs created by the graph.
playableGraph.Destroy();
}
}
The PlayableGraph
generated by `MixAnimationSample
Blending an AnimationClip and AnimatorController
This example demonstrates how to use an AnimationMixerPlayable
to blend an AnimationClip
with an AnimatorController
.
Before blending the AnimationClip
and AnimatorController
, they must be wrapped by playables. To do this, an AnimationClipPlayable
(clipPlayable) wraps the AnimationClip
(clip) and an AnimatorControllerPlayable
(ctrlPlayable) wraps the RuntimeAnimatorController (controller). The SetInputWeight(
) method dynamically adjusts the blend weight of each playable.
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
[RequireComponent(typeof(Animator))]
public class RuntimeControllerSample : MonoBehaviour
{
public AnimationClip clip;
public RuntimeAnimatorController controller;
public float weight;
PlayableGraph playableGraph;
AnimationMixerPlayable mixerPlayable;
void Start()
{
// Creates the graph, the mixer and binds them to the Animator.
playableGraph = PlayableGraph.Create();
var playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
mixerPlayable = AnimationMixerPlayable.Create(playableGraph, 2);
playableOutput.SetSourcePlayable(mixerPlayable);
// Creates AnimationClipPlayable and connects them to the mixer.
var clipPlayable = AnimationClipPlayable.Create(playableGraph, clip);
var ctrlPlayable = AnimatorControllerPlayable.Create(playableGraph, controller);
playableGraph.Connect(clipPlayable, 0, mixerPlayable, 0);
playableGraph.Connect(ctrlPlayable, 0, mixerPlayable, 1);
// Plays the Graph.
playableGraph.Play();
}
void Update()
{
weight = Mathf.Clamp01(weight);
mixerPlayable.SetInputWeight(0, 1.0f-weight);
mixerPlayable.SetInputWeight(1, weight);
}
void OnDisable()
{
// Destroys all Playables and Outputs created by the graph.
playableGraph.Destroy();
}
}
Creating a PlayableGraph with several outputs
This example demonstrates how to create a PlayableGraph
with two different playable output types: an AudioPlayableOutpu
t and an AnimationPlayableOutput
. A PlayableGraph
can have many playable outputs of different types.
This example also demonstrates how to play an AudioClip
through an AudioClipPlayable
that is connected to an AudioPlayableOutput
.
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Audio;
using UnityEngine.Playables;
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(AudioSource))]
public class MultiOutputSample : MonoBehaviour
{
public AnimationClip animationClip;
public AudioClip audioClip;
PlayableGraph playableGraph;
void Start()
{
playableGraph = PlayableGraph.Create();
// Create the outputs.
var animationOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
var audioOutput = AudioPlayableOutput.Create(playableGraph, "Audio", GetComponent<AudioSource>());
// Create the playables.
var animationClipPlayable = AnimationClipPlayable.Create(playableGraph, animationClip);
var audioClipPlayable = AudioClipPlayable.Create(playableGraph, audioClip, true);
// Connect the playables to an output
animationOutput.SetSourcePlayable(animationClipPlayable);
audioOutput.SetSourcePlayable(audioClipPlayable);
// Plays the Graph.
playableGraph.Play();
}
void OnDisable()
{
// Destroys all Playables and Outputs created by the graph.
playableGraph.Destroy();
}
}
The PlayableGraph
generated by `MultiOutputSample
Controlling the play state of the tree
This example demonstrates how to use the Playable.SetPlayState()
method to control the play state of node on the PlayableGraph
tree. The SetPlayState
method controls the play state of the entire tree, one of its branches, or a single node.
When setting the play state of a node, the state propagates to all its children, regardless of their play states. For example, if a child node is explicitly paused, setting a parent node to “playing” also sets all its child nodes to “playing.”
In this example, the PlayableGraph
contains a mixer that blends two animation clips. An AnimationClipPlayable
wraps each animation clip and the SetPlayState()
method explicitly pauses the second playable. The second AnimationClipPlayable
is explicitly paused, so its internal time does not advance and outputs the same value. The exact value depends on the specific time when the AnimationClipPlayable
was paused.
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
[RequireComponent(typeof(Animator))]
public class PauseSubGraphAnimationSample : MonoBehaviour
{
public AnimationClip clip0;
public AnimationClip clip1;
PlayableGraph playableGraph;
AnimationMixerPlayable mixerPlayable;
void Start()
{
// Creates the graph, the mixer and binds them to the Animator.
playableGraph = PlayableGraph.Create();
var playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
mixerPlayable = AnimationMixerPlayable.Create(playableGraph, 2);
playableOutput.SetSourcePlayable(mixerPlayable);
// Creates AnimationClipPlayable and connects them to the mixer.
var clipPlayable0 = AnimationClipPlayable.Create(playableGraph, clip0);
var clipPlayable1 = AnimationClipPlayable.Create(playableGraph, clip1);
playableGraph.Connect(clipPlayable0, 0, mixerPlayable, 0);
playableGraph.Connect(clipPlayable1, 0, mixerPlayable, 1);
mixerPlayable.SetInputWeight(0, 1.0f);
mixerPlayable.SetInputWeight(1, 1.0f);
clipPlayable1.SetPlayState(PlayState.Paused);
// Plays the Graph.
playableGraph.Play();
}
void OnDisable()
{
// Destroys all Playables and Outputs created by the graph.
playableGraph.Destroy();
}
}
The PlayableGraph generated by PauseSubGraphAnimationSample. Notice that the second clip is paused (red edge).
Controlling the timing of the tree
This example demonstrates how to use the Play() method to play a PlayableGraph, how to use the SetPlayState() method to pause a playable, and how to use the SetTime() method to manually set the local time of a playable with a variable.
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
[RequireComponent(typeof(Animator))]
public class PlayWithTimeControlSample : MonoBehaviour
{
public AnimationClip clip;
public float time;
PlayableGraph playableGraph;
AnimationClipPlayable playableClip;
void Start()
{
playableGraph = PlayableGraph.Create();
var playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
// Wrap the clip in a playable
playableClip = AnimationClipPlayable.Create(playableGraph, clip);
// Connect the Playable to an output
playableOutput.SetSourcePlayable(playableClip);
// Plays the Graph.
playableGraph.Play();
// Stops time from progressing automatically.
playableClip.SetPlayState(PlayState.Paused);
}
void Update ()
{
// Control the time manually
playableClip.SetTime(time);
}
void OnDisable()
{
// Destroys all Playables and Outputs created by the graph.
playableGraph.Destroy();
}
}
Creating PlayableBehaviour
This example demonstrates how to create custom playables with the PlayableBehaviour
public class. This example also demonstrate how to override the PrepareFrame()
virtual method to control nodes on the PlayableGraph
. Custom playables can override any of the other virtual methods of the PlayableBehaviour
class.
In this example, the nodes being controlled are a series of animation clips (clipsToPlay). The SetInputMethod()
modifies the blend weight of each animation clip, ensuring that only one clip plays at a time, and the SetTime()
method adjusts the local time so playback starts at the moment the animation clip is activated.
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
public class PlayQueuePlayable : PlayableBehaviour
{
private int m_CurrentClipIndex = -1;
private float m_TimeToNextClip;
private Playable mixer;
public void Initialize(AnimationClip[] clipsToPlay, Playable owner, PlayableGraph graph)
{
owner.SetInputCount(1);
mixer = AnimationMixerPlayable.Create(graph, clipsToPlay.Length);
graph.Connect(mixer, 0, owner, 0);
owner.SetInputWeight(0, 1);
for (int clipIndex = 0 ; clipIndex < mixer.GetInputCount() ; ++clipIndex)
{
graph.Connect(AnimationClipPlayable.Create(graph, clipsToPlay[clipIndex]), 0, mixer, clipIndex);
mixer.SetInputWeight(clipIndex, 1.0f);
}
}
override public void PrepareFrame(Playable owner, FrameData info)
{
if (mixer.GetInputCount() == 0)
return;
// Advance to next clip if necessary
m_TimeToNextClip -= (float)info.deltaTime;
if (m_TimeToNextClip <= 0.0f)
{
m_CurrentClipIndex++;
if (m_CurrentClipIndex >= mixer.GetInputCount())
m_CurrentClipIndex = 0;
var currentClip = (AnimationClipPlayable)mixer.GetInput(m_CurrentClipIndex);
// Reset the time so that the next clip starts at the correct position
currentClip.SetTime(0);
m_TimeToNextClip = currentClip.GetAnimationClip().length;
}
// Adjust the weight of the inputs
for (int clipIndex = 0 ; clipIndex < mixer.GetInputCount(); ++clipIndex)
{
if (clipIndex == m_CurrentClipIndex)
mixer.SetInputWeight(clipIndex, 1.0f);
else
mixer.SetInputWeight(clipIndex, 0.0f);
}
}
}
[RequireComponent(typeof (Animator))]
public class PlayQueueSample : MonoBehaviour
{
public AnimationClip[] clipsToPlay;
PlayableGraph playableGraph;
void Start()
{
playableGraph = PlayableGraph.Create();
var playQueuePlayable = ScriptPlayable<PlayQueuePlayable>.Create(playableGraph);
var playQueue = playQueuePlayable.GetBehaviour();
playQueue.Initialize(clipsToPlay, playQueuePlayable, playableGraph);
var playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
playableOutput.SetSourcePlayable(playQueuePlayable);
playableOutput.SetSourceInputPort(0);
playableGraph.Play();
}
void OnDisable()
{
// Destroys all Playables and Outputs created by the graph.
playableGraph.Destroy();
}
}
The PlayableGraph generated by PlayQueueSample
/
///