Audio tutorial for Unity: the Audio Mixer

Learn how to use the Audio Mixer in Unity to create immersive audio experiences for your games. This tutorial covers everything you need to know. By Jeff Fisher.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 4 of this article. Click here to view the first page.

Audio Manager

Note: This section assumes you know how to edit code in a code editor and that you have a basic knowledge of C#. Check out some of our other Unity tutorials first if you need to sharpen your programming skills in Unity.

With the Audio Mixer complete, it’s time to give the player some options in game that will let them make their own changes to the Audio Mixer.

In the Hierarchy window, there is a game object named AudioManager that has a child canvas object named AudioManagerCanvas. Under that, you should see another object that is currently inactive named SliderPanel. Enable this game object.

Exposing Parameters

In the Game window you should see some sliders and buttons. These will be the sliders to adjust the Music and SFX volumes.

In order to access the volume levels of your Music and SFX group, you need to expose those parameters. To do this, first select the Music group, then in the Inspector window, right click the Volume field and select the Expose ‘Volume (of Music)’ to script option.

Note: You can expose just about any field in the Audio Mixer, including the value fields of effects.

Now select the SFX group and expose its Volume as well. Back in the Audio Mixer window, you should now see Exposed Parameters (2) at the top right.

Click on this and rename the newly exposed parameters respectively to musicVol and sfxVol. These are the names you’ll use to access them in scripts.

Select the AudioManager game object. In the Inspector window, navigate to the Audio Manager script. This contains an array of Audio Settings.

Each element within the array has three values. The first two are for the sliders functionality, but the third is the exposed parameter that the slider influences. Type in the exposed parameter names where musicVol should be under the element containing the MusicSlider. sfxVol should be under the SFXSlider.

With that done, open up the AudioManager.cs script. The first thing you need to add is a reference to your Audio Mixer. At the top of the script, underneath public static AudioManager instance; add the following line:

    public AudioMixer mixer;

Save the script, then return to the Unity Editor. A new field named Mixer should be on the Audio Manager component. Drag your MasterMixer from the Project window into this empty variable. This will give you a reference to your Audio Mixer so you can change its exposed parameters.

Open AudioManager.cs again, scroll down to the AudioSetting class, and just below the Initialize() method, add this new method:

public void SetExposedParam(float value) // 1
{
    redX.SetActive(value <= slider.minValue); // 2
    AudioManager.instance.mixer.SetFloat(exposedParam, value); // 3
    PlayerPrefs.SetFloat(exposedParam, value); // 4
}

Here's what that code does:

  1. A public method that takes in a float for the new value.
  2. A red X sprite that is set active when sound is completely disabled.
  3. The call to the Audio Mixer that sets the exposed parameter to the specified value.
  4. A PlayerPref to remember the users volume choice.

In the main AudioManager class, create the following two public methods that your sliders will hook to:

    public void SetMusicVolume(float value)
    {
        audioSettings[(int)AudioGroups.Music].SetExposedParam(value);
    }

    public void SetSFXVolume(float value)
    {
        audioSettings[(int)AudioGroups.SFX].SetExposedParam(value);
    }

Save the script. Head back to the Unity Editor and navigate to the MusicSlider game object.

Find the Slider component attached to the Music Slider object in the Inspector window. Then hook the AudioManager.SetMusicVolume dynamically to the slider.

Then select the SFXSlider game object and do the same thing, but hook it dynamically to AudioManager.SetSFXVolume.

Selecting Dynamic float passes in the current value of the slider to the method.

Note: Make sure you hook the methods as a Dynamic float and not a Static Parameter.

Save and run the project. You should now be able to adjust the sliders in-game to change the volume levels of the Music and SFX groups.

Congratulations! You just exposed and modified your first Audio Mixer fields.

Snapshots

Now it's time to give functionality to the two buttons that have been waiting patiently in the wings. These will be used to transition between Snapshots in your Audio Mixer.

The first thing you need is another Snapshot, which is basically a saved state of your Audio Mixer. For this example, you'll be creating a new Snapshot that focuses the audio levels to favor the Music group.

Click the + next to the Snapshots header, and name your new Snapshot MusicFocus, as the focus of the Snapshot will be on the Music group. Then rename the first Snapshot to Starting as it will be the default Snapshot that will be used, as indicated by the star icon to the right of the name.

With the MusicFocus Snapshot selected, adjust the Music group volume to be louder than the SFX group volume. You should now be able to jump back and forth between Snapshots, and the values for each Snapshot should be automatically saved.

With the setup done, you can dive back into the AudioManager script, and add the following new instance variables to the top of the script.

private AudioMixerSnapshot startingSnapshot; // 1
private AudioMixerSnapshot musicFocusSnapshot; // 2   

Here's what they do:

  1. A variable used to reference the starting snapshot.
  2. A variable used to reference the music focus snapshot.

At the top of the Start() method, just before the for loop, add:

startingSnapshot = mixer.FindSnapshot("Starting"); // 1
musicFocusSnapshot = mixer.FindSnapshot("MusicFocus"); // 2 
  1. Finds the Starting snapshot on the audio mixer and stores it in the startingSnapshot reference variable.
  2. Finds the MusicFocus snapshot on the audio mixer and stores it in the musicFocusSnapshot reference variable.

Then create two new methods in the AudioManager class that will be used to transition between each snapshot:

public void SnapshotStarting()
{
    startingSnapshot.TransitionTo(.5f);
}

public void SnapshotMusic()
{
    musicFocusSnapshot.TransitionTo(.5f);
}

TransitionTo will interpolate from the current snapshot to the invoking snapshot over a specified time interval.

Save your script and return to the Unity Editor. Find the StartingSnapShot game object in the Hierarchy window.

In the Inspector window under the Button component, hook the button into the newly created SnapshotStarting() method.

Then do the same with the MusicFocusSnapshot game object, but with the SnapshotMusic() method.

Save and run your project. When you click the Music Focus button, you should see the volume of the Music group transition up, and the SFX group transition down. You can then transition back to the starting snapshot by clicking the Starting Snapshot button.

Note: In order for the Snapshots to transition correctly, they must be appropriately aligned on start. If you made adjustments to the volume sliders, make sure the Starting Snapshot is at the same volume as it's corresponding volume slider — otherwise the new Snapshot values will be ignored during the transition.
Jeff Fisher

Contributors

Jeff Fisher

Author

Gijs Bannenberg

Tech Editor

Chris Belanger

Editor

Sean Duffy

Final Pass Editor

Over 300 content creators. Join our team.