Volume control system for music and sound effects for Unity

Introduction

This solution for Unity consists of a volume control system for music and sound effects, the idea is that in a configuration window the desired levels are adjusted, the system saves that information and applies it to all the AudioSource components that are registered. To register an AudioSource component in the system just assign one of two scripts that come in the package, one is assigned if it is a music AudioSource, the other if the AudioSource is a sound effect AudioSource. The system will register all these AudioSources, adding those that are created and removing those that are destroyed. In addition, as the system saves the configuration set by the user, when starting the game these saved volumes will be applied.

Unity package to download and import:

How to use this volume control system for Unity

In the download package there is a scene in which the system is assembled.

Step 1: Place the AudioSettings script in the scene for system control

In the package comes a Script called AudioSettings, this Script has to be present in the scene, in my case I put it in an empty GameObject called “AudioSettings”, in the inspector you will see the current values of music volume and sound effects and the amount of registered AudioSources, but these values are only to know what is happening inside the system, writing values in those fields will not affect anything.

Step 2: Assign appropriate Scripts to AudioSources

In a certain scene we will have several AudioSource components that will produce sounds. To register these AudioSources it is necessary to assign a Script to the same GameObject that has the AudioSource, which is in charge of initializing these objects and integrating them to the system, the scripts are: “MusicAudioSource” and “SFXAudioSource”, just by assigning one of these scripts the system takes care of integrating them, load the appropriate volume and modify the volume when there is a change in the configuration, no matter if the object with the AudioSource exists before entering the game mode or is instantiated through a Prefab (as long as we put the Script in the Prefab). We can check this by looking at the AudioSettings script inspector, which tells us how many AudioSources of each type are registered.

Step 3: Volume control sliders

In the scene comes a small Canvas containing two control Sliders, one for the volume of the music and one for the volume of the sound effects. These Sliders have to execute the appropriate dynamic volume change functions that are defined within the AudioSettings Script (see the “OnValueChanged” event of each Slider).

To make the Slider initially show the volume values with which the system is loaded, you must have the “UIElementInitializer” Script assigned and select the type of element it is, in this case we will have only two elements: “MUSIC_Slider” and “SFX_Slider”, but you can extend this system to initialize any type of element of the user interface you want, simply by adding elements to the enum inside the Script and adding cases inside the Switch defined in the Start function.

This would allow you to have volume control sliders in different scenes and have them all affect the same settings.

Scroll to Top
Secured By miniOrange