How to do it...

To add volume control sliders to your scene, follow these steps:

  1. Create a new Unity 3D project.
  2. Import the provided Unity package, Volume, into your project.
  3. Open the Volume scene from Project panel folder, VolumeControl | _Scenes.
  4. Play the scene and walk toward the semitransparent green wall in the tunnel, using the W A S D keys (press Shift to run). You will be able to listen to:
    • A looping music soundtrack
    • Bells ringing
    • A robotic speech whenever the character collides with the wall
  5. In the Project panel, use the Create menu to add an Audio Mixer file. Rename this new file MainMixer.
  6. Double-click MainMixer to open the Audio Mixer panel.
  1. In the the Audio Mixer panel Groups section, highlight Master and click the + (plus) sign to add a child to the Master group. Name this child Music. Repeat these actions to add a second child of Master named FX:
  1. In the the Audio Mixer panel Mixers section, highlight MainMixer and click the + (plus) sign to add a new item to the Mixers group. Rename this MusicMixer (you may need to rename this via the Project panel, since you've created a new Audio Mixer file through this process).
  2. Drag MusicMixer onto MainMixer (child it), and select Music as its output in the pop-up dialog window.
  3. In the the Audio Mixer panel Mixers section, highlight MainMixer and click the + (plus) sign to add a new item to the Mixers group. Name this FxMixer.
  4. Child (drag) FxMixer onto MainMixer, and select Fx as its output in the pop-up dialog window.
  5. Select MusicMixer. Select its Master group and add a child named Soundtrack.
  6. Select FxMixer and add two children to its Master group: one named Speech, and another named Bells:
  1. Select the DialogueTrigger GameObject in the Hierarchy. In the Inspector, change its Audio Source component Output track to FxMixer | Speech:
  1. Select the Soundtrack GameObject in the Hierarchy. In the Inspector, change its Audio Source component Output track to MusicMixer | Soundtrack.
  2. Select Signal from the Prefabs folder in the Project panel. In the Inspector, change its Audio Source component Output track to FxMixer | Bells.

  1. Select MainMixer in the Audio Mixer panel, and select its Master track. In the Inspector panel, right-click on Volume in the Attenuation component. From the pop-up-context menu, select Expose Volume (of Master) to script:
  1. Repeat the operation to expose the Volume to scripts for both the Music and FX Groups.
  2. At the top-right of the Audio Mixer panel, you should see Exposed Parameters (three). Click the drop-down icon, and rename them as follows: MyExposedParam to OverallVolume; MyExposedParam1 to MusicVolume, and MyExposedParam2 to FxVolume. Note the sequence of the three parameters may not match the order you added them, so double-check that the greyed-out track names on the right correspond correctly:
  1. In the Hierarchy panel, use the Create drop-down menu to add a UI Panel to the scene (menu: Create | UI | Panel). Unity will automatically add a Canvas parent for this panel.
  2. In the Hierarchy panel, create a UI Slider to the scene (menu: Create | UI | Slider). Make it a child of the Panel object. Rename this slider as Slider-overall. Set the slider's Min Value to 0.000025 (or 2.5e-05).
  3. Duplicate it and rename the new copy to Slider-music. In the Inspector panel, Rect Transform component, change its Pos Y parameter to -40.
  4. Duplicate Slider-music and rename the new copy to Slider-fx. Change its Pos Y parameter to -70:
  1. Create a C# script class, VolumeControl, in the _Scripts folder, containing the following code, and add an instance as a scripted component to the Main Camera GameObject:
using UnityEngine; 
using UnityEngine.Audio; 
 
public class VolumeControl : MonoBehaviour { 
    public GameObject panel; 
   public AudioMixer myMixer; 
   private bool isPaused = false; 
 
   void Start(){ 
        panel.SetActive(false); 
 
        ON_CHANGE_OverallVol(0.01F); 
        ON_CHANGE_MusicVol(0.01F); 
        ON_CHANGE_FxVol(0.01F); 
   } 
 
   void Update() { 
         if (Input.GetKeyUp (KeyCode.Escape)) { 
               panel.SetActive(!panel.activeInHierarchy); 
 
               if(isPaused) 
                     Time.timeScale = 1.0f; 
               else 
                     Time.timeScale = 0.0f; 
 
               isPaused = !isPaused; 
         }            
   }      
 
    public void ON_CHANGE_OverallVol(float vol) { 
        myMixer.SetFloat("OverallVolume", Mathf.Log10(vol) * 20f); 
    } 
 
   public void ON_CHANGE_MusicVol(float vol) { 
         myMixer.SetFloat ("MusicVolume", Mathf.Log10(vol) * 20f); 
   } 
 
   public void ON_CHANGE_FxVol(float vol) { 
         myMixer.SetFloat ("FxVolume", Mathf.Log10(vol) * 20f); 
   } 
} 
  1. With Main Camera selected in the Hierarchy panel, drag the Panel GameObject into the Inspector for the public Panel variable.
  2. With Main Camera selected in the Hierarchy panel, drag MainMixer from the Project panel into the Inspector for the public My Mixer variable.
  3. Select the OverallSlider component. Below the On Value Changed list, click the + sign to add an action. From the Hierarchy panel, drag Main Camera into the Object slot and using the drop-down menu, choose VolumeControl | ON_CHANGE_OverallVol option.  For testing purposes, change the appropriate selector from Runtime Only to Editor and Runtime.
  4. Repeat the previous step with MusicSlider and FxSlider, but this time, choose the ON_CHANGE_MusicVol and ON_CHANGE_FxVol options, respectively, from the drop-down menu.
  5. Play the scene. You will be able to access the sliders when pressing ESCAPE on your keyboard and adjust volume settings from there.