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
.
Before you use the DifferentOutputs
script in your project, your project must have the following:
RequireComponent
attribute adds these components if they are not present.To use the DifferentOutputs
script in your project, follow these steps:
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();
}
}
In the Script component, select the Animation Clip and Audio 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 MultiOutputSample
to display the PlayableGraph.
DifferentOutputs
script.