Game options – toggling audio

In this recipe, we will show you how toggle buttons for the user interface of games made with Unity can be created and managed.

Getting ready

Like the previous recipe, you must have the Options Screen scene loaded in the Editor and the GUI script open in Monodevelop.

How to do it...

  1. Let's begin by creating a bool variable to control whether the toggle is on or off. We do this by adding the following declaration at the top of the script:
    private bool toggleAudio = true;
  2. Next, we create the toggle button on the screen by adding the following line inside the OnGUI() function:
    toggleAudio = GUI.Toggle(new Rect(100, 30, 10, 10), toggleAudio, "Toggle Audio");
  3. Now, in the Update() function, we add a line to ensure that the audio is enabled, depending on the toggleAudio value:
    audio.enabled = toggleAudio;
  4. The following screenshot provides the complete GUI script we used for the Options Screen scene:
    How to do it...
..................Content has been hidden....................

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