Version: 6000.3
语言: 中文
控制可播放片段的时序
创建自定义试玩对象

创建具有不同输出的 PlayableGraph

此示例演示了如何创建PlayableGraph具有两种不同的可播放输出类型:一个AudioPlayableOutputAnimationPlayableOutput.此示例还演示了如何播放AudioClip通过AudioClipPlayable连接到AudioPlayableOutput.

先决条件

在使用DifferentOutputs脚本,则项目必须具有以下内容:

  • 一个游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
    请参阅术语表
    例如立方体或胶囊。您无需手动添加Animator 组件模型上的一个组件,使用动画系统为该模型设置动画。该组件具有对控制动画的 Animator 控制器资源的引用。更多信息
    请参阅术语表
    音频源(Audio Source) 将场景中的音频剪辑播放到音频监听器或通过混音器播放的组件。更多信息
    请参阅术语表
    组件添加到此游戏对象。这RequireComponent属性会添加这些组件(如果它们不存在)。
  • 对游戏对象的属性进行动画处理的动画剪辑。例如,更改游戏对象位置和旋转的动画。如果你的项目没有动画剪辑,你将无法选择剪辑,并且在运行时不会发生任何事情。
  • 音频剪辑Unity 中音频数据的容器。Unity 支持单声道、立体声和多声道音频资产(最多 8 个声道)。Unity 可以导入 .aif、.wav、.mp3 和 .ogg 音频文件格式,以及 .xm、.mod、.it 和 .s3m 跟踪器模块格式。更多信息
    请参阅术语表
    .如果项目没有音频剪辑,则在游戏对象动画处理时将无法播放音频。

添加并运行脚本

要使用DifferentOutputsscript,请按照以下步骤作:

  1. 将脚本组件添加到游戏对象。为脚本文件命名DifferentOutputs.cs并使用以下代码:

    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. 在 脚本(Script) 组件中,选择 PlayableGraph 将在运行时播放的 动画剪辑(Animation Clip) 和 音频剪辑(Audio Clip) 。

  3. 选择 运行(Play) 将编辑器切换到播放模式。

  4. 如果您已安装 PlayableGraph Visualizer 包,请选择MultiOutputSample以显示 PlayableGraph。

由 DifferentOutputs 脚本生成的 PlayableGraph。
DifferentOutputs脚本。

其他资源

控制可播放片段的时序
创建自定义试玩对象