Version: Unity 6.3 Beta (6000.3)
Language : English
Control the timing of a playable clip
Create a custom playable

Create a PlayableGraph with different outputs

This example demonstrates how to create a PlayableGraph with two different playable output types: an AudioPlayableOutput and an AnimationPlayableOutput. This example also demonstrates how to play an AudioClip through an AudioClipPlayable connected to an AudioPlayableOutput.

Prerequisites

Before you use the DifferentOutputs 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
    or an Audio SourceA component which plays back an Audio Clip in the scene to an audio listener or through an audio mixer. More info
    See in Glossary
    component to this GameObject. The RequireComponent attribute adds these components if they are not present.
  • An animation clip that animates the properties of the GameObject. For example, an animaton that changes the position and rotation of a GameObject. If your project doesn’t have an animation clip, you will not be able to select a clip and nothing will happen at runtime.
  • An audio clipA container for audio data in Unity. Unity supports mono, stereo and multichannel audio assets (up to eight channels). Unity can import .aif, .wav, .mp3, and .ogg audio file format, and .xm, .mod, .it, and .s3m tracker module formats. More info
    See in Glossary
    . If your project doesn’t have an audio clip, you will not be able to play audio while the GameObject animates.

Add and run the script

To use the DifferentOutputs script in your project, follow these steps:

  1. Add a script component to your GameObject. Name the script file DifferentOutputs.cs and use the following code:

    using UnityEngine;
    using UnityEngine.Animations;
    using UnityEngine.Audio;
    using UnityEngine.Playables;
    
    [RequireComponent(typeof(Animator))]
    [RequireComponent(typeof(AudioSource))]
    public class DifferentOutputs : MonoBehaviour
    {
        public AnimationClip animationClip;
        public AudioClip audioClip;
        PlayableGraph graph;
    
        void Start()
        {
            // Create and name the graph.
            graph = PlayableGraph.Create("DifferentOutputs");
    
            // Create the outputs.
            var animationOutput = AnimationPlayableOutput.Create(graph, "Animation", GetComponent<Animator>());
            var audioOutput = AudioPlayableOutput.Create(graph, "Audio", GetComponent<AudioSource>());
    
            // Create the playables.
            var animationClipPlayable = AnimationClipPlayable.Create(graph, animationClip);
            var audioClipPlayable = AudioClipPlayable.Create(graph, audioClip, true);
    
            // Connect the playables to an output.
            animationOutput.SetSourcePlayable(animationClipPlayable);
            audioOutput.SetSourcePlayable(audioClipPlayable);
    
            // Play the Graph.
            graph.Play();
        }
    
        void OnDisable()
        {
            // Destroys all Playables and Outputs created by the graph.
            graph.Destroy();
        }
    }
    
  2. In the Script component, select the Animation Clip and Audio 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 MultiOutputSample to display the PlayableGraph.

The PlayableGraph generated by the DifferentOutputs script.
The PlayableGraph generated by the DifferentOutputs script.

Additional resources

Control the timing of a playable clip
Create a custom playable