Version: Unity 6.3 Beta (6000.3)
Language : English
PlayableGraph Visualizer
Blend two animation clips

Play an animation clip

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.

Prerequisites

To use the PlayAnimationClip script in your project, your project must have the following:

  • A GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
    See in Glossary
    such as a cube or a capsule. You don’t need to manually add an Animator componentA component on a model that animates that model using the Animation system. The component has a reference to an Animator Controller asset that controls the animation. More info
    See in Glossary
    to this GameObject. The RequireComponent attribute adds this component if it is not present.
  • 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 PlayAnimationClip script in your project, follow these steps:

  1. 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();
        }
    }
    
  2. In the Script component, select the animation clip (clip) that the PlayableGraph will play at runtime.

  3. Select Play to switch the Editor to Play mode.

  4. If you have installed the PlayableGraph Visualizer package, select PlayAnimationClip to display the PlayableGraph:

The PlayableGraph generated by the PlayAnimationClip script
The PlayableGraph generated by the PlayAnimationClip script

Additional resources

  • Animation clipsAnimation data that can be used for animated characters or simple animations. It is a simple “unit” piece of motion, such as (one specific instance of) “Idle”, “Walk” or “Run”. More info
    See in Glossary
  • Animator ControllerControls animation through Animation Layers with Animation State Machines and Animation Blend Trees, controlled by Animation Parameters. The same Animator Controller can be referenced by multiple models with Animator components. More info
    See in Glossary
  • Creating scripts
  • Play mode in the Game view
PlayableGraph Visualizer
Blend two animation clips