Volume control system for music and sound effects for Unity

Introduction

This solu­tion for Uni­ty con­sists of a vol­ume con­trol sys­tem for music and sound effects, the idea is that in a con­fig­u­ra­tion win­dow the desired lev­els are adjust­ed, the sys­tem saves that infor­ma­tion and applies it to all the AudioSource com­po­nents that are reg­is­tered. To reg­is­ter an AudioSource com­po­nent in the sys­tem just assign one of two scripts that come in the pack­age, one is assigned if it is a music AudioSource, the oth­er if the AudioSource is a sound effect AudioSource. The sys­tem will reg­is­ter all these AudioSources, adding those that are cre­at­ed and remov­ing those that are destroyed. In addi­tion, as the sys­tem saves the con­fig­u­ra­tion set by the user, when start­ing the game these saved vol­umes will be applied.

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

Unity package to download and import:

Music and SFX Vol­ume Con­trol for Unity

How to use this volume control system for Unity

In the down­load pack­age there is a scene in which the sys­tem is assembled.

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

In the pack­age comes a Script called AudioSet­tings, this Script has to be present in the scene, in my case I put it in an emp­ty GameOb­ject called "AudioSet­tings", in the inspec­tor you will see the cur­rent val­ues of music vol­ume and sound effects and the amount of reg­is­tered AudioSources, but these val­ues are only to know what is hap­pen­ing inside the sys­tem, writ­ing val­ues in those fields will not affect anything.

Step 2: Assign appropriate Scripts to AudioSources

In a cer­tain scene we will have sev­er­al AudioSource com­po­nents that will pro­duce sounds. To reg­is­ter these AudioSources it is nec­es­sary to assign a Script to the same GameOb­ject that has the AudioSource, which is in charge of ini­tial­iz­ing these objects and inte­grat­ing them to the sys­tem, the scripts are: "Musi­cAu­dioSource" and "SFX­Au­dioSource", just by assign­ing one of these scripts the sys­tem takes care of inte­grat­ing them, load the appro­pri­ate vol­ume and mod­i­fy the vol­ume when there is a change in the con­fig­u­ra­tion, no mat­ter if the object with the AudioSource exists before enter­ing the game mode or is instan­ti­at­ed through a Pre­fab (as long as we put the Script in the Pre­fab). We can check this by look­ing at the AudioSet­tings script inspec­tor, which tells us how many AudioSources of each type are registered.

Step 3: Volume control sliders

In the scene comes a small Can­vas con­tain­ing two con­trol Slid­ers, one for the vol­ume of the music and one for the vol­ume of the sound effects. These Slid­ers have to exe­cute the appro­pri­ate dynam­ic vol­ume change func­tions that are defined with­in the AudioSet­tings Script (see the "OnVa­l­ueChanged" event of each Slider).

To make the Slid­er ini­tial­ly show the vol­ume val­ues with which the sys­tem is loaded, you must have the "UIEle­men­tIni­tial­iz­er" Script assigned and select the type of ele­ment it is, in this case we will have only two ele­ments: "MUSIC_Slider" and "SFX_Slider", but you can extend this sys­tem to ini­tial­ize any type of ele­ment of the user inter­face you want, sim­ply by adding ele­ments to the enum inside the Script and adding cas­es inside the Switch defined in the Start function.

This would allow you to have vol­ume con­trol slid­ers in dif­fer­ent scenes and have them all affect the same settings.

Scroll to Top
Secured By miniOrange