Creating an Options menu

Something that many games also need is an Options menu, so let's create it by performing the following steps:

  1. First, let's prepare Pause Menu to support an Options screen. Select the Pause Menu object and create an empty child called Bottom Row and then add a Horizontal Group Layout component to it.
  2. Duplicate the Main Menu button and then drag and drop the original Main Menu and Quit Game to Bottom Row. Afterward, rename the Main Menu copy Options and change the text as well to reflect that.
    Creating an Options menu
  3. Now, we will need to create the Options menu. Duplicate the Pause Menu object and rename it Options Menu.
  4. Then, move the Options button to the bottom and rename it Back and then changes the names of Quit and Main Menu to Decrease and Increase Quality.
  5. Next, we want to add a slider to the menu to allow players to change the volume of the game. To do that, go to GameObject | UI | Slider and then drag it on top of Options Menu to add it to the menu.
  6. Select the Options Menu object and set Height to 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.
    Creating an Options menu
  7. Next, we're going to need to write some functions and add a new property to our PauseMenuBehaviour, so with that in mind open it up and add the following variable:
    public GameObject optionsMenu;
  8. Then, update the script to have the following new functions:
    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.

    Note

    For more info on the ToString function, check out: https://msdn.microsoft.com/en-us/library/f71z6k0c(v=vs.110).aspx.

  9. Then, we need to have to update the Start function of have the following bolded lines:
    public void Start()
    {
        isPaused = false;
        pauseMenu.SetActive(false);
     optionsMenu.SetActive(false);
    
        UpdateQualityLabel();
        UpdateVolumeLabel();
    }
  10. Save the script and move back into the Unity editor. From there, go to Pause Menu Behaviour and set the Options Menu property to our Options Menu object.
  11. Next, select the Slider object and scroll down to the Slider component and add an On Value Changed event with Pause Menu Behaviour attached and call the Dynamic float version of SetVolume.
    Creating an Options menu
  12. Afterward, change the Value of the Slider component to 1 to reflect full volume.
  13. Then, have Decrease and Increase Quality call their respective functions.
  14. Next, we need to be able to go from Options to the Pause menu and vice versa, so add in two more functions for us to work with in Pause Menu Behaviour:
    public void OpenOptions()
        {
            optionsMenu.SetActive(true);
            pauseMenu.SetActive(false);
        }
    
        public void OpenPauseMenu()
        {
            optionsMenu.SetActive(false);
            pauseMenu.SetActive(true);
        }
  15. Then, assign OpenPauseMenu to the Back button and OpenOptions to the Options button on the Pause menu.
  16. Then, we need to adjust our Escape button behavior in 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();
                }
                
            }
        }
  17. It looks fine now, but we also want our text to look easy to read, so go to the Options Menu object and change the Image component's Color to have an Alpha to 200. Then, do the same to the Pause Menu panel.
  18. Lastly, hide the Options Menu object from the Hierarchy tab.
  19. After this, we save all of our files and then go back into Unity and run the game, as shown in the following screenshot:
    Creating an Options menu

    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.

    Creating an 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:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset