Something that many games also need is an Options menu, so let's create it by performing the following steps:
200
to account for us adding new things and then put a Text
object above the Slider and Quality markers, saying Master Volume and Quality Level.public GameObject optionsMenu;
public void IncreaseQuality() { QualitySettings.IncreaseLevel(); UpdateQualityLabel(); } public void DecreaseQuality() { QualitySettings.DecreaseLevel(); UpdateQualityLabel(); } public void SetVolume(float value) { AudioListener.volume = value; UpdateVolumeLabel(); } private void UpdateQualityLabel() { int currentQuality = QualitySettings.GetQualityLevel(); string qualityName = QualitySettings.names[currentQuality]; optionsMenu.transform.FindChild("Quality Level").GetComponent<UnityEngine.UI.Text>().text = "Quality Level - " + qualityName; } private void UpdateVolumeLabel() { optionsMenu.transform.FindChild("Master Volume").GetComponent<UnityEngine.UI.Text>().text = "Master Volume - " + (AudioListener.volume * 100).ToString("f2") + "%"; }
In the following code we use the ToString function to convert the AudioListener's volume property (a float) into a string which we can print. We provide "f2" to the function to say we want to format the text as a float with 2 decimal points.
For more info on the ToString function, check out: https://msdn.microsoft.com/en-us/library/f71z6k0c(v=vs.110).aspx.
public void Start() { isPaused = false; pauseMenu.SetActive(false); optionsMenu.SetActive(false); UpdateQualityLabel(); UpdateVolumeLabel(); }
On Value Changed
event with Pause Menu Behaviour attached and call the Dynamic float version of SetVolume
.public void OpenOptions() { optionsMenu.SetActive(true); pauseMenu.SetActive(false); } public void OpenPauseMenu() { optionsMenu.SetActive(false); pauseMenu.SetActive(true); }
OpenPauseMenu
to the Back button and OpenOptions
to the Options button on the Pause menu.Update
:public void Update() { if (Input.GetKeyUp("escape")) { if (!optionsMenu.activeInHierarchy) { // If false becomes true and vice-versa isPaused = !isPaused; // If isPaused is true, 0 otherwise 1 Time.timeScale = (isPaused) ? 0 : 1; pauseMenu.SetActive(isPaused); } else { OpenPauseMenu(); } } }
Over the course of this section, we've done two things. First of all, we changed the pause menu to now have an additional Options menu.
And when we click on the Options button, we will see a separate menu that will allow users to modify the Master Volume value as well as the Graphics Quality value of their project. This is shown in the following screenshot: