UI Volume Control Graphic

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

In Articles by John8 Comments

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 -6db. 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 Example
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.0001 – 1
    instead of -80db – 0db (the 0.0001 is important, and stops the slider breaking at zero)
  • When passing the slider value to the SetFloat function, convert it using Mathf.Log10(value) * 20;
    e.g. mixer.SetFloat(“MusicVol”, Mathf.Log10(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.0001 (the logarithmic conversion will not work correctly at zero)
  3. Set the Value to 1
    Unity Volume Slider Inspector Settings

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.Log10(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

Comments

  1. Thank you so much for this! It’s so obvious once you’ve explained it so clearly 🙂

  2. I was curious if you had a similar scaling function for a Low Pass filter cutoff. Since the values are in Herz and not decibels, the numbers are different but I can’t quite figure it out. Thanks!

  3. Just a heads up, using 0.001 as the slider minimum will only allow the minimum volume to go to -60 dB. You’ll want an extra zero in there to make it go all the way to -80 dB.

    log(0.001) = -3 * 20 = -60
    log(0.0001) = -4 * 20 = -80

    1. John Author

      Thanks for this. I actually hadn’t experienced this issue at all with Math.Log and a minimum value of 0.001 however this does happen when using Log10, as has been suggested in another comment so thank you for your advice, I’ve updated the article.

    1. John Author

      Thanks for this, I’ve now tried this and I’ve updated the article to use Log10 instead as, when used with a minimum value of 0.0001, which Marshall suggested in another comment, creates a slightly smoother slider travel than before with an exact -80db lower limit (my original method would bottom out at more than -100db). Thanks for your advice.

Leave a Comment