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)

How to do it...

To create a UI Slider and display its value on the screen, follow these steps:

  1. Create new Unity 2D project.
  2. Add a UI Text GameObject to the scene with a Font size of 30 and placeholder text, such as Slider value here (this text will be replaced with the slider value when the scene starts). Set Horizontal- and Vertical- Overflow to Overflow.
  3. In the Hierarchy window, add a UI Slider GameObject to the scene by going to GameObject | UI | Slider.
  4. In the Inspector window, modify the settings for the position of the UI Slider GameObject's Rect Transform to the top-middle part of the screen.
  5. In the Inspector window, modify the settings of Position for the UI Text's Rect Transform so that they're just below the slider (top, middle, then Pos Y = -30).
  6. In the Inspector window, set the UI Slider's Min Value to 0 and Max Value to 20. Then, check the Whole Numbers checkbox:
Figure 2.14 – Setting the UI Slider's Min Value and Max Value
  1. Create a C# script class called SliderValueToText containing the following code and add an instance as a scripted component to the Text GameObject:
using UnityEngine; 
using UnityEngine.UI; 

public class SliderValueToText : MonoBehaviour { 
   public Slider sliderUI; 
   private Text textSliderValue; 

   void Awake() {
         textSliderValue = GetComponent<Text>(); 
   } 

   void Start() { 
         ShowSliderValue(); 
   } 

   public void ShowSliderValue () { 
         string sliderMessage = "Slider value = " + sliderUI.value; 
         textSliderValue.text = sliderMessage; 
   } 
} 
  1. Ensure that the Text GameObject is selected in the Hierarchy window. Then, in the Inspector window, drag the Slider GameObject into the public Slider UI variable slot for the Slider Value To Text (Script) scripted component:
Figure 2.15 – Dragging Slider into the Slider UI variable
  1. Ensure that the Slider GameObject is selected in the Hierarchy window. Then, in the Inspector window, drag the Text GameObject from the Hierarchy window over to the Object slot (immediately below the menu that says Runtime Only, as shown in the following screenshot:
Figure 2.16 – Dragging the Text GameObject into None (Object)
Registering an object to receive UI event messages

You have now told Unity which object a message should be sent to each time the slider is changed.

  1. From the drop-down menu, select SliderValueToText and the ShowSliderValue method, as shown in the following screenshot. This means that each time the slider is updated, the ShowSliderValue() method, in the scripted object in the Text GameObject, will be executed:
Figure 2.17 – Drop-down menu for On Value Changed (Single)
  1. When you run scene, you will see a UI Slider. Below it, you will see a text message in the form Slider value = <n>.
  2. Each time UI Slider is moved, the text value that's shown will be (almost) instantly updated. The values should range from 0 (the leftmost of the slider) to 20 (the rightmost of the slider).