How it works...

The Audio Mixer feature works in a similar fashion to Digital Audio Workstations, such as Logic and Sonar. Through Audio Mixers you can organize and manage audio elements by routing them into specific groups that can have individual audio tracks you can tweak, allowing adjustments in volume level and sound effects.

By organizing and routing our audio clips into two groups (Music and FX), we established MainMixer as a unified controller for volume. Then, we used the Audio Mixer to expose the volume levels for each track of MainMixer, making them accessible to our script.

Also, we have set up a basic UI featuring three sliders that, when in use, will pass their float values (between 0.000025 and 1) as arguments to three specific functions in our script: ON_CHANGE_MusicVol, ON_CHANGE_FxVol, and ON_CHANGE_OverallVol. These functions, on their turn, use the SetFloat command to effectively change the volume levels at runtime. However, before passing on the new volume levels, the script converts linear values (between 0.000025 and 1) to the decibel levels that are used by the Audio Mixer. This conversion is calculated through the log(x) * 20 mathematical function.

For a full explanation on issues regarding the conversion of linear values to decibel levels and vice-versa, check out Aaron Brown's excellent article at  http://www.playdotsound.com/portfolio-item/decibel-db-to-float-value-calculator-making-sense-of-linear-values-in-audio-tools/.

If one of the sliders seems not to work, double-check that the name of the first parameter to SetFloat(...) matches the exposed parameter in the AudioMixer  any spelling difference will mean the on-change function will not change values in the Audio Mixer. For example, if the named exposed parameter was wrongly named "OveralVolume" (missing an "l"), this statement in response to a slider change would not work due to the spelling mismatch:

myMixer.SetFloat ("OverallVolume ", Mathf.Log10(vol) * 20f); 

The VolumeControl script includes code to enable and disable the UI and the EventSystem, depending upon whether the player hits the ESCAPE key to activate/deactivate the volume-control sliders.

Since our VolumeControl script sets the maximum volume level for the music and Fx tracks, you should not manually change any of the track volumes of the MainMixer at Design-Time. For general adjustments, use the secondary  MusicMixer and FxMixer mixers.