UI Volume Control in Unity

The right way to make a volume slider in Unity (using logarithmic conversion)

In Articles by JohnLeave a Comment

In Unity it’s fairly straightforward to create a UI slider volume control, using exposed parameters, the Audio Mixer and a simple script.This is useful for creating UI volume controls for your player.

If you haven’t already, check out Unity’s official tutorial here, you’ll also find written steps later in this post.

But be sure to come back after…

…because although the standard method kind of works, it misses out a pretty crucial step:

The slider value is linear but the mixer value, the attenuation, is logarithmic. Connecting the two, without converting the value first, makes the slider far too sensitive.

Why is this a problem?

If you reduce the slider to about halfway, you’ll find that the audio isn’t half as loud (as you might expect), in fact it’s virtually silent.

This is because the volume range of the Audio Group fader, which is in decibels, is logarithmic meaning that the increments are exponential. Each increment on the scale represents a greater and greater value: e.g. 10, 100, 1000 and so on. In comparison the slider scale is linear and the increments are evenly spaced: e.g. 1, 2, 3.

Normally we perceive half volume to be at around -10db. However, moving the slider to halfway doesn’t do this. In fact, at halfway, it actually sets a value of -40db which is almost at the bottom.

The difference is clear when comparing the two methods side by side:

Unity Volume Slider Gif - Difference between logarithmic conversion and linear value
On the far right is the fader response using the standard ‘direct’ method. To the left of this is a fader using the ‘converted’ method

What’s the quick fix?

If you already have this issue, and you just want to fix it, here’s what to do:

  • Use a slider value range of 0.001 – 1
    instead of -80db – 0db (the 0.001 is important, and stops the slider breaking at zero)
  • When passing the slider value to the SetFloat function, convert it using Mathf.Log(value) * 20;
    e.g. mixer.SetFloat(“MusicVol”, Mathf.Log(sliderValue) * 20);

For full instructions, including this fix, follow the steps below.

How to create a UI volume control slider in Unity (that actually works)

Create the slider

  1. In the hierarchy select Create > UI > Slider to create a UI Slider for your volume control
  2. Select the Slider object and set the Min Value to 0.001 (the logarithmic conversion will not work correctly at zero)
  3. Set the Value to 1
    Slider Controls in Unity

Expose the Audio Mixer Group volume to scripting

  1. Select the Audio Mixer Group that you want to control with the slider
  2. In the inspector, under attenuation, right click on the volume label and select Expose ‘Volume (of Music)’ to script
    Expose Parameter in Unity
  3. In the Audio Mixer Panel, select the Exposed Parameters dropdown and give the, now exposed, volume parameter a name. e.g. “MusicVol”. You’ll need to use the parameter name given here when accessing it from a script.
    Exposed Parameters Menu

Connect the slider with a script

  1. Add a new C Sharp script to the slider object called SetVolume or similar.
  2. Open it for editing and type, or paste, the following script:
  3. Save the script and return to Unity.
  4. Use circle select to connect the mixer reference variable to the Mixer that contains the exposed parameter.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;

public class SetVolume : MonoBehaviour {

	public AudioMixer mixer;

	public void SetLevel (float sliderValue)
		mixer.SetFloat("MusicVol", Mathf.Log(sliderValue) * 20);

Important: If you’re just copying the function, not the whole script, remember to add the using UnityEngine.Audio namespace at the top of the script. This is required to access Audio Mixer functions.

Add the function to the slider

  1. Select the slider object and add a new ‘On value changed’ event.
  2. Drag the SetVolume script to the field or set it by using circle select.
  3. Select the function dropdown, where it currently reads ‘no function’. Select the SetVolume script and then select the SetLevel function (listed under Dynamic float*).
    Connect slider to volume control function in Unity

* If the function doesn’t appear under the dynamic float list make sure that 1. the access modifier for the function is set to public and 2. that the function takes a single float parameter.

Save the scene and hit play to test

The slider should now control the Audio Mixer Group fader.

Repeat to create music and audio volume controls. You can even use multiple mixer groups to give players more granular control over voices, sound effects, music and other sound categories.

Share this Post

Leave a Comment