Book Image

Unity 2021 Cookbook - Fourth Edition

By : Shaun Ferns
Book Image

Unity 2021 Cookbook - Fourth Edition

By: Shaun Ferns

Overview of this book

If you are a Unity developer looking to explore the newest features of Unity 2021 and recipes for advanced challenges, then this fourth edition of Unity Cookbook is here to help you. With this cookbook, you’ll work through a wide variety of recipes that will help you use the essential features of the Unity game engine to their fullest potential. You familiarize yourself with shaders and Shader Graph before exploring animation features to enhance your skills in building games. As you progress, you will gain insights into Unity's latest editor, which will help you in laying out scenes, tweaking existing apps, and building custom tools for augmented reality and virtual reality (AR/VR) experiences. The book will also guide you through many Unity C# gameplay scripting techniques, teaching you how to communicate with database-driven websites and process XML and JSON data files. By the end of this Unity book, you will have gained a comprehensive understanding of Unity game development and built your development skills. The easy-to-follow recipes will earn a permanent place on your bookshelf for reference and help you build better games that stay true to your vision.
Table of Contents (15 chapters)
Free Chapter
2
Responding to User Events for Interactive UIs
3
Inventory and Advanced UIs
6
2D Animation and Physics
13
Advanced Topics - Gizmos, Automated Testing, and More
15
Virtual and Augmented Reality (VR/AR)

There's more...

Unity UI Toggles are also the base components if we wish to implement a group of mutually exclusive options in the style of radio buttons. We need to group related radio buttons together (UI Toggles) to ensure that when one radio button turns on (is selected), all the other radio buttons in the group turn off (unselected).

We also need to change the visual look if we want to adhere to the usual style of radio buttons as circles, rather than the square UI Toggle default images:

Figure 2.28 – Example of three buttons with Console status

To create a group of related toggles in the visual style of radio buttons, do the following to the project you just created:

  1. Import the UI Demo Textures folder into the project.
  2. Remove the C# script class's ToggleChangeManager component from the Toggle GameObject.
  3. Rename the Toggle GameObject Toggle-easy.
  4. Select the Canvas GameObject and, in the Inspector window, add a UI | Toggle Group component.
  5. With the Toggle-easy GameObject selected, in the Inspector window, drag the Canvas GameObject into the Toggle Group property of the Toggle (Script) component.
  6. Change the Label text to Easy and tag this GameObject with a new tag called Easy. Do this by selecting Add Tag from the Tag drop-down menu in the Inspector window, then typing in Easy, then selecting the GameObject again and setting its tag to Easy.
  7. Select the Background child GameObject of Toggle-easy and, in the Image (Script) component, drag the UIToggleBG image into the Source Image property (a circle outline).
  1. Ensure that the Is On property of the Toggle (Script) component is checked, and then select the Checkmark child GameObject of Toggle-easy. In the Image (Script) component, drag the UIToggleButton image into the Source Image property (a filled circle).
Of the three choices (easy, medium, and hard) that we'll offer to the user, we'll set the easy option to be the one that is supposed to be initially selected. Therefore, we need its Is On property to be checked, which will lead to its checkmark image being displayed.

 

To make these toggles look more like radio buttons, the background of each is set to the circle outline image of UIToggleBG, and the checkmark (which displays the toggles that are on) is filled with the circle image called UIToggleButton.

  1. Duplicate the Toggle-easy GameObject, naming the copy Toggle-medium. Set its Rect Transform property's Pos Y to -25 (so that this copy is positioned below the easy option) and uncheck the Is On property of the Toggle (Script) component. Tag this copy with a new tag called Medium.
  2. Duplicate the Toggle-medium GameObject, naming the copy Toggle-hard. Set its Rect Transform property's Pos Y to -50 (so that this copy is positioned below the medium option). Tag this copy with a new tag called Hard.
  3. Add an instance of the RadioButtonManager C# script class to the Canvas GameObject:
using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 

public class RadioButtonManager : MonoBehaviour { 
  private string currentDifficulty = "Easy"; 

  public void PrintNewGroupValue(Toggle sender){ 
    // only take notice from Toggle just switched to On 
    if(sender.isOn){ 
      currentDifficulty = sender.tag; 
      print ("option changed to = " + currentDifficulty); 
    } 
  } 
}
  1. Select the Toggle-easy GameObject in the Project window. Now, do the following:
    • Since we based this on the First Class toggle, there is already an On Value Changed event for the list of event handlers for the Toggle (Script) component. Drag the Canvas GameObject in the target object slot (under the drop-down showing Runtime Only).
    • From the Function drop-down menu, choose RadioButtonManager, and then choose the PrintNewGroupValue method.
    • In the Toggle parameter slot, which is initially None (Toggle), drag the Toggle-easy GameObject. Your On Value Changed settings in the Inspector window should look as follows:
Figure 2.29 – Dragging the Toggle-easy GameObject to the Toggle parameter slot
  1. Do the same for the Toggle-medium and Toggle-hard GameObjects so that each Toggle object calls the PrintNewGroupValue(...) method of a C# scripted component called RadioButtonManager in the Canvas GameObject, passing itself as a parameter.
  2. Save and run the scene. Each time you check one of the three radio buttons, the On Value Changed event will fire, and you'll see a new text message printed into the Console window by our script, stating the tag of whichever Toggle (radio button) was just set to true (Is On).

By adding a Toggle Group component to Canvas, and having each Toggle GameObject link to it, the three radio buttons can tell Toggle Group when they have been selected. Then, the other members of the group are deselected. If you had several groups of radio buttons in the same scene, one strategy is to add the Toggle Group component to one of the toggles and have all the others link to that one.

We store the current radio button value (the last one switched On) in the currentDifficulty class property. Since variables declared outside a method are remembered, we could, for example, add a public method, such as GetCurrentDifficulty(), which could tell other scripted objects the current value, regardless of how long it's been since the user last changed their option.