Sound volume adjustment can be a very important feature, especially if your game is a standalone. After all, it can be very frustrating to access the operational system volume control. In this recipe, we will use the new Audio Mixer feature to create independent volume controls for Music and Sound FX.
For this recipe, we have provided a Unity package named Volume.unitypackage
, containing an initial scene featuring soundtrack music and sound effects. The file is available inside the 1362_09_05
folder.
To add volume control sliders to your scene, follow these steps:
Volume.unitypackage
into your project.OverallVolume
. Then, rename MyExposedParam1 as MusicVolume
and MyExposedParam2 as FxVolume
.VolumeControl
.using UnityEngine; using UnityEngine.Audio; using System.Collections; public class VolumeControl : MonoBehaviour{ public AudioMixer myMixer; private GameObject panel; private bool isPaused = false; void Start(){ panel = GameObject.Find("Panel"); panel.SetActive(false); } 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 ChangeMusicVol(float vol){ myMixer.SetFloat ("MusicVolume", Mathf.Log10(vol) * 20f); } public void ChangeFxVol(float vol){ myMixer.SetFloat ("FxVolume", Mathf.Log10(vol) * 20f); } public void ChangeOverallVol(float vol){ myMixer.SetFloat ("OverallVolume", Mathf.Log10(vol) * 20f); } }
-40
.-70
:0.000025
(or 2.5e-05). Then, below the On Value Changed list, click the + sign to add an action. From Hierarchy panel, drag Canvas into the Object slot and using the drop-down menu, choose VolumeControl | ChangeOverallVol option, as shown in the following screenshot, For testing purposes, change the appropriate selector from Runtime Only to Editor and Runtime.The new 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 to be tweaked around, allowing for adjustments in volume level and sound effects.
By organizing and routing our audio clips into two groups (Music and FX), we established the MainMixer as a unified controller for volume. Then, we have used the Audio Mixer to expose the volume levels for each track of the MainMixer, making them accessible to our script.
Also, we have set up a basic GUI 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: ChangeMusicVol
, ChangeFxVol
, and ChangeOverallVol
. 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/.
It's worth mentioning that the VolumeControl
script also includes code to enable and disable the GUI and the EventSystem, depending upon if the player hits the Escape key to activate/deactivate the volume control sliders.
A very important note—do not change the volume of any MainMixer's tracks; leave them at 0
dB. The reason is that our VolumeControl
script sets their maximum volume level. For general adjustments, use the secondary Mixers MusicMixer and FxMixer.
3.141.192.183