Honkbark studios menu logo

unity3d game volume

Unity3d game volume

Please share this post with your friends

There is a global volume setting for unity games that is simple to use. It’s called AudioListener.volume. In this example we will toggle the volume on and off in a game by the click of a button.

The code in this example is published on Github and after each step you can have a look at how the code looks by following the “Github commit from step…” link. You can also download the example project, and you will find this example in the Scenes/Volume scene.

This technique is used in Friendsheep.

1. Start by adding music to the scene.  I used a free stock song from JewelBeat in this example. Download it here. Add it to your project, click on Main Camera in the hiearchy then add component and add an audio source.
Now drag the file you added to the project to the audioclip on the newly added audio source component. It should now look like this:
Volume-01Github commit from step 1

 

2. Now add a button to handle the turning on and off the volume.
Click on GameObject -> UI -> Button
Change name on the button to ToggleSound and change the text of the button to turn music off.
Volume-02

 

Github commit from step 2

 

3. Add a new script to ToggleSound called AudioHandlerController and open that file in your editor.

Github commit from step 3

 

4.  To mute and unmute the sounds in this scene we will use AudioListener.volume
The volume property is a float that controls the game sound volume (0.0 to 1.0).

Create a new method called ToggleMusic(). This method will check if the AudioListener.volume is 0.0f and if so set it to 1.0, and vice versa. The script will look like this:

using UnityEngine;
using System.Collections;

public class AudioHandlerController : MonoBehaviour {

    public void ToggleMusic() {
        if(AudioListener.volume == 0.0f) {
            AudioListener.volume = 1.0f;
        }
        else if(AudioListener.volume == 1.0f) {
            AudioListener.volume = 0.0f;
        }
    }
}

Github commit from step 4

 

5.  Now let’s hook this up to the button to fire ToggleMusic when user clicks it.
Click on the ToggleSound button in the hierarchy and click on the plus sign(+) at the bottom of the On Click() tab.

Drag the ToggleSound Game object from the hierarchy to where it says None(Object) in the On Click() tab.

Now the dropdown updates so click on it where it says no function, go down to AudioHandlerController and select ToggleMusic.

Volume-03

And now that’s what we need to do in order to make the button mute and unmute the music!

Github commit from step 5

 

6. This is a bonus. Let’s change the text of the button when we click it.

In the AudioHandlerController add a new using statement: UnityEngine.UI.
Also add a public property of the type text called ButtonText;

Now create a new method that takes string parameter called UpdateButtonText(string value)
This method will set the ButtonText.Text property to a new value everytime the user clicks it, to do this our code could look like this:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class AudioHandlerController : MonoBehaviour {

    public Text ButtonText;

    public void ToggleMusic() {
        if(AudioListener.volume == 0.0f) {
            AudioListener.volume = 1.0f;
            this.UpdateButtonText("off");
        }
        else if(AudioListener.volume == 1.0f) {
            AudioListener.volume = 0.0f;
            this.UpdateButtonText("on");
        }
    }
    
    private void UpdateButtonText(string value) {
        ButtonText.text = "Turn music " + value;
    }
}

And lastly drag the text GameObject from the hierarchy to the new property of our script in unity.
volume-04

 

Github commit from step 6

Categories: C#,Tutorial,Unity3D