Exercise - Enable and disable spatialization at run time

Completed

In this unit, you'll learn how to enable and disable spatialization at run time and test spatialization in the Unity editor and HoloLens 2.

Add spatialization control script

  1. Right-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.

    Screenshot of creating a script.

  2. Double-click the script in the Project window to open it in Visual Studio. Replace the default script contents with the following:

    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;
        }
    }
    

    Note

    To enable or disable the spatialization, the script only adjusts the spatialBlend property, leaving the spatialization property enabled. In this mode, Unity still applies the Volume curve. Otherwise, if the user were to disable spatialization when far from the source, they would hear the volume increase abruptly.

    If you prefer to fully disable spatialization, modify the script to also adjust the SourceObject variable's Spatialization boolean property.

Attach your script and drive it from the button

  1. Select Quad in the Hierarchy. In the Inspector window, use the Add Component button to add SpatializeOnOff(Script)

    Screenshot of Add script to quad.

  2. In the Hierarchy window, locate PressableButton_32x32mm_IconAndTextUnder > TextLabelUnder. With the Quad object still selected in the Hierarchy, in the Inspector window, locate the Spatialize On Off (Script) component and drag and drop TextLabelUnder component to the Button Text Object field.

    Screenshot of finding the Quad object in the hierarchy, with the SpatialierOnOff component.

  3. To set the button to call the SpatializeOnOff script when the button is released, you need to configure interactable script. In the Hierarchy window, select the PressableButton_32x32mm_IconAndTextUnder. In the Inspector window, locate the Pressable Button component and select the + icon under the OnClicked () event.

    1. With the PressableButton_32x32mm_IconAndTextUnder 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.

    2. Select the No Function drop-down, then select SpatializeOnOff > SwapSpatialization () to turn the spatial audio on and off.

    Screenshot of Button action settings.