4. 在執行時間啟用和停用 spatialization4. Enabling and disabling spatialization at run time

概觀Overview

在本教學課程中,您將瞭解如何在執行時間啟用和停用 spatialization,並在 unity 編輯器和 HoloLens 2 中進行測試。In this tutorial, you will learn how to Enable and disable spatialization at run time and test this in the unity editor and HoloLens 2.

目標Objectives

  • 新增腳本來控制遊戲物件上的 spatializationAdd a new script to control spatialization on a game object
  • 從按鈕動作驅動 spatialization 控制腳本Drive the spatialization control script from button actions

新增 spatialization 控制項腳本Add spatialization control script

以滑鼠右鍵按一下 [專案] 視窗,然後選擇 [建立 > c # 腳本] 來建立新的 c # 腳本,輸入適合腳本的名稱,例如 SpatializeOnOffRight-click in the Project window and choose Create > C# Script to create a new C# script, enter a suitable name for the script, for example, SpatializeOnOff:

建立腳本

按兩下 [專案] 視窗中的腳本,在 Visual Studio 中開啟它。Double-click the script in the Project window to open it in Visual Studio. 以下列內容取代預設腳本內容:Replace the default script contents with the following:

注意

腳本的幾行批註化。下一章將取消批註這些行 :使用回音來增加空間音訊的距離Several lines of the script are commented out. These lines will be uncommented in Next Chapter: Using reverb to add distance to spatial audio.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;

[RequireComponent(typeof(AudioSource))]
public class SpatializeOnOff : MonoBehaviour
{
    public GameObject ButtonTextObject;
    //public AudioMixerGroup RoomEffectGroup;
    //public AudioMixerGroup MasterGroup;

    private AudioSource m_SourceObject;
    private bool m_IsSpatialized;
    private TMPro.TextMeshPro m_TextMeshPro;

    public void Start()
    {
        m_SourceObject = gameObject.GetComponent<AudioSource>();
        m_TextMeshPro = ButtonTextObject.GetComponent<TMPro.TextMeshPro>();
        SetSpatialized();
    }

    public void SwapSpatialization()
    {
        if (m_IsSpatialized)
        {
            SetStereo();
        }
        else
        {
            SetSpatialized();
        }
    }

    private void SetSpatialized()
    {
        m_IsSpatialized = true;
        m_SourceObject.spatialBlend = 1;
        m_TextMeshPro.SetText("Set Stereo");
        //m_SourceObject.outputAudioMixerGroup = RoomEffectGroup;
    }

    private void SetStereo()
    {
        m_IsSpatialized = false;
        m_SourceObject.spatialBlend = 0;
        m_TextMeshPro.SetText("Set Spatialized");
        //m_SourceObject.outputAudioMixerGroup = MasterGroup;
    }

}

注意

若要啟用或停用 spatialization,腳本只會調整 spatialBlend 屬性,並啟用 spatialization 屬性。To enable or disable the spatialization, the script only adjusts the spatialBlend property, leaving the spatialization property enabled. 在此模式中,Unity 仍會套用 音量 曲線。In this mode, Unity still applies the Volume curve. 否則,如果使用者從來源到目前為止都要停用 spatialization,他們會聽到大量增加。Otherwise, if the user were to disable spatialization when far from the source, they would hear the volume increase abruptly. 如果您想要完全停用 spatialization,請修改腳本,以同時調整 SourceObject 變數的 spatialization 布林值屬性。If you prefer to fully disable spatialization, modify the script to also adjust the spatialization boolean property of the SourceObject variable.

附加您的腳本,並從按鈕進行驅動Attach your script and drive it from the button

選取階層中的 [ ],然後在 [偵測器] 視窗中,使用 [新增元件] 按鈕來新增 SpatializeOnOff (腳本)Select Quad in the Hierarchy and in the Inspector window, use the Add Component button to add SpatializeOnOff(Script)

將腳本新增至四個

在階層中,找到 PressableButtonHoloLens2 > IconAndText > TextMeshProIn the Hierarchy locate PressableButtonHoloLens2 > IconAndText > TextMeshPro.

在階層中仍選取了 個物件時,請在 [偵測器] 視窗中,找 出 (腳本) 元件的 Spatialize,然後拖放 PressableButtonHoloLens2 的 TextMeshPro 元件。With the Quad object still selected in the Hierarchy, in the Inspector window, locate the Spatialize On Off (Script) component and Drag and drop TextMeshPro Component of the PressableButtonHoloLens2.

尋找階層中的 PressableButtonHoloLens2 物件

若要在放開按鈕時設定按鈕以呼叫 SpatializeOnOff 腳本,您必須設定互動腳本。To set the button to call the SpatializeOnOff script when the button is released You need to configure interactable script.

在 [階層] 視窗中,選取 PressableButtonHoloLens2In the Hierarchy window, select the PressableButtonHoloLens2. 在 [偵測器] 視窗中,找出 [ 互動 (腳本) 元件],然後按一下 [OnClick ( # A3 事件] 底下的 + 圖示。In the Inspector window, locate the Interactable (Script) component and click on + icon under OnClick () event.

  • 在 [階層] 視窗中仍選取 [ PressableButtonHoloLens2 ] 物件,然後從 [階層] 視窗中,將 [ 個物件] 拖曳至您剛剛加入之事件的 [ 無 (物件]) 欄位,讓 ButtonParent 物件從此按鈕接聽按鈕 click 事件:With the PressableButtonHoloLens2 object still selected in the Hierarchy window, click-and-drag the Quad object from the Hierarchy window into the empty None (Object) field of the event you just added to make the ButtonParent object listen for button click event from this button:

  • 按一下相同事件的 [沒有函式] 下拉式清單。Click the No Function dropdown of the same event. 然後選取 SpatializeOnOff > SwapSpatialization ( # B1 來開啟和關閉空間音訊Then select SpatializeOnOff > SwapSpatialization () to turn on and off the Spatial audio

按鈕動作設定

恭喜!Congratulations

在本教學課程中,您已瞭解如何在執行時間啟用和停用 spatialization、在 HoloLens 2 上或在 Unity 編輯器中測試應用程式。In this tutorial, you have learned how to enable and disable spatialization at run time, test out the app on a HoloLens 2 or in the Unity editor. 您現在可以在應用程式中按一下按鈕,以啟用和停用音訊的 spatialization。In the app, you can now click the button to activate and deactivate spatialization of the audio.

在下一個教學課程中,您將會新增一個回音效果,讓音效成為距離的意義。In the next tutorial you'll add a reverb effect to give sounds a sense of distance.

提示

如需有關如何建立 Unity 專案並將其部署至 HoloLens 2 的提醒,您可以參閱對您的 HoloLens 2 建置應用程式的指示。For a reminder on how to build and deploy your Unity project to HoloLens 2, you can refer to the Building your app to your HoloLens 2 instructions.