5.Playable创建多输出的PlayableGraph
5.1 知识点
思路和步骤
用 Playable API 创建多输出的 PlayableGraph,核心就是:一张图里挂多个输出口,分别绑定 Animator 和 AudioSource,让动画与音频同步驱动。
核心步骤:
- 创建图并设置更新模式
- 创建动画输出和音频输出(分别绑定到 Animator 和 AudioSource)
- 创建动画剪辑和音频剪辑节点
- 连接节点到对应的输出
- 播放图
- 清理资源
代码实现
步骤1:创建图并设置更新模式
// 创建 Playable 图并设置更新模式为 GameTime
playableGraph = PlayableGraph.Create();
playableGraph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
先创建 PlayableGraph,并设置时间更新模式为游戏时间。
步骤2:创建动画输出和音频输出
// 创建动画输出,连接到 Animator 组件
AnimationPlayableOutput animationPlayableOutput =
AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
// 创建音频输出,连接到 AudioSource 组件
AudioPlayableOutput audioPlayableOutput =
AudioPlayableOutput.Create(playableGraph, "Audio", GetComponent<AudioSource>());
创建两个输出节点:动画输出绑到 Animator,音频输出绑到 AudioSource。一张图同时驱动两路输出。
步骤3:创建动画剪辑和音频剪辑节点
// 创建动画剪辑的可播放项
AnimationClipPlayable animationClipPlayable = AnimationClipPlayable.Create(playableGraph, animationClip);
// 创建音频剪辑的可播放项
// AudioClipPlayable.Create 参数说明:
// 参数1:PlayableGraph(要创建的图)
// 参数2:AudioClip(要播放的音频剪辑)
// 参数3:loop(是否循环播放,true 表示循环)
AudioClipPlayable audioClipPlayable = AudioClipPlayable.Create(playableGraph, audioClip, true);
把动画剪辑和音频剪辑都包装成 Playable 节点。AudioClipPlayable.Create() 的第三个参数控制是否循环播放。
步骤4:连接节点到对应的输出
// 将可播放项连接到对应的输出
animationPlayableOutput.SetSourcePlayable(animationClipPlayable);
audioPlayableOutput.SetSourcePlayable(audioClipPlayable);
把动画剪辑节点接到动画输出,音频剪辑节点接到音频输出。图开始播放后,两路同步更新。
步骤5:播放图
// 播放该图
playableGraph.Play();
开始播放图,之后每帧 Unity 会自动更新,动画与音频同步推进。
步骤6:清理资源
void OnDisable()
{
// 销毁该图创建的所有可播放项和输出
playableGraph.Destroy();
}
组件禁用时调用 Destroy() 释放资源(节点与输出),避免泄漏。建议放在 OnDisable(),禁用时即可释放。
效果展示


5.2 知识点代码
MultiOutputSample.cs
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Audio;
using UnityEngine.Playables;
/// <summary>
/// 多输出 PlayableGraph 示例组件
/// 该组件演示了如何创建一个包含多个输出的 PlayableGraph
/// 同时输出动画和音频,实现动画与音频的同步播放
/// </summary>
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(AudioSource))]
public class MultiOutputSample : MonoBehaviour
{
/// <summary>
/// 要播放的动画剪辑
/// </summary>
public AnimationClip animationClip;
/// <summary>
/// 要播放的音频剪辑
/// </summary>
public AudioClip audioClip;
/// <summary>
/// Playable 图,用于管理动画和音频播放流程
/// </summary>
PlayableGraph playableGraph;
/// <summary>
/// 初始化并创建多输出的 PlayableGraph
/// </summary>
void Start()
{
// 创建 Playable 图并设置更新模式为 GameTime
playableGraph = PlayableGraph.Create();
playableGraph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
// 创建动画输出,连接到 Animator 组件
AnimationPlayableOutput animationPlayableOutput =
AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
// 创建音频输出,连接到 AudioSource 组件
AudioPlayableOutput audioPlayableOutput =
AudioPlayableOutput.Create(playableGraph, "Audio", GetComponent<AudioSource>());
// 创建动画剪辑的可播放项
AnimationClipPlayable animationClipPlayable = AnimationClipPlayable.Create(playableGraph, animationClip);
// 创建音频剪辑的可播放项
// AudioClipPlayable.Create 参数说明:
// 参数1:PlayableGraph(要创建的图)
// 参数2:AudioClip(要播放的音频剪辑)
// 参数3:loop(是否循环播放,true 表示循环)
AudioClipPlayable audioClipPlayable = AudioClipPlayable.Create(playableGraph, audioClip, true);
// 将可播放项连接到对应的输出
animationPlayableOutput.SetSourcePlayable(animationClipPlayable);
audioPlayableOutput.SetSourcePlayable(audioClipPlayable);
// 播放该图
playableGraph.Play();
}
/// <summary>
/// 组件禁用时清理资源,销毁 Playable 图及其所有可播放项和输出
/// </summary>
void OnDisable()
{
// 销毁该图创建的所有可播放项和输出
playableGraph.Destroy();
}
}
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com