Creating a Settings Menu in 10 minutes
Objective: Create a Pause Menu that pauses the game and can change volume and brightness of scene.
To start we will need an Empty Parent object to hold all the UI objects. This used two sliders and one button. The rest is just regular TextMeshPro text.
Now for the scripts, we will need a GameManager script, PauseMenu, and Spawner for the balls dropping in game to show the scene is pausing. I’ll cover the Spawner first since it is the quickest. All we need for this is a variable for the prefab sphere, a spawn point and a bool. In Start we will start a Coroutine. In the IEnumerator we will check while the bool is true, instantiate the ball at the spawnpoint, cast it to a game object and wait for 1 second. Then destroy it’s self after another second.
Pause Game.
For pausing the game and bringing up the pause menu we will go into the game manager. Need to grab a reference to the PauseMenu game object and set a bool variable.
Next we will create a public method for activating the menu. We will check to see if _menuActive is true, if it is activate menu, if not deactivate menu.
Now we will create another public method, this one will control pausing everything in game and calling the switch method. First we will see if menuActive is true, set to false. If false set to true.
Check to see if the menuActive is true or false again, if it is true call the switchmenu method and set Time.timeScale to 0. This will stop everything in scene except the menu. If false, call the SwitchMenu() again and return Time.timeScale back to 1.
All that’s left in GameManager is to check for Input from the player. If the player hits the Escape key call the ClosePauseMenu() Method.
Back in Unity just assign the empty Parent object on to the GameManager Script.
Change Brightness
There are two way’s to change brightness in a game. One is to use postprocessing and adjust a value. For a quick behind the scenes magic we can use a black image overlay. Create an Image and make sure it is set to the full screen.
Now in our PauseMenu script we will need a variable for this object. Since we will be using an Image, we will need to grab the UnityEngine.UI library. We can go ahead and create the slider as well.
Create a public method for changing the brightness. We will need to adjust the Alpha channel of the image. Just calling _screenOverlay.Color.a = slidervalue doesn’t work. We can’t change the return value of a graphic.color.
To adjust this we will need to create a temp variable in the method set it equal to the _screenOverlay.color. Than we will change the tempColor.a to equal the slider value. Finally set the _screenOverlay.color to equal the tempColor.
Assign the image in the script.
Now select the brightness slider and onvalue change add in the pause menu and select the correct method.
With that we can darken our scene or brighten it.
Adjust Audio Levels.
We will use Audio Mixers for this part. To open the audio mixer go to Window → Audio → AudioMixer. Or just CRTL+8.
Create a new group by pressing the plus symbol by Groups.
Select the new Group and in the inspector, right click on Volume and expose variable.
Back in the Audio Mixer window, in the top right you will see Exposed Parameters. Here you can change the name of the variable to make it easier to remember.
On an object that will hold the audio source, drop in an audio clip. Make sure Play On Awake and Loop is set to true. For OutPut select the new group one we created.
Back in the Pause menu script we will need a reference to the AudioMixer and the audio slider. For the AudioMixer we will need the UnityEngine.Audio library.
Create a public method for adjusting the audio. In here all we need to do is call the bgVolume.SetFloat(“pass in the name of the exposed parameter”, set the value);. We will set the value by passing in the volume slider.
Back in Unity, drop in the slider and Audio Mixer into the script.
The last thing that needs to be done is create a new event in OnValueChanged and pass in the pausemenu script and select the method. We will want to set the min and max value. Min should be -80 and max should be 0.
And just like that we have a working Pause Menu!
Thank you for reading! Have a wonderful day!