Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 2018 Cookbook

You're reading from   Unity 2018 Cookbook Over 160 recipes to take your 2D and 3D game development to the next level

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher
ISBN-13 9781788471909
Length 794 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Matt Smith Matt Smith
Author Profile Icon Matt Smith
Matt Smith
Francisco Queiroz Francisco Queiroz
Author Profile Icon Francisco Queiroz
Francisco Queiroz
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Preface 1. Displaying Data with Core UI Elements FREE CHAPTER 2. Responding to User Events for Interactive UIs 3. Inventory UIs 4. Playing and Manipulating Sounds 5. Creating Textures, Maps, and Materials 6. Shader Graphs and Video Players 7. Using Cameras 8. Lights and Effects 9. 2D Animation 10. 3D Animation 11. Webserver Communication and Online Version-Control 12. Controlling and Choosing Positions 13. Navigation Meshes and Agents 14. Design Patterns 15. Editor Extensions and Immediate Mode GUI (IMGUI) 16. Working with External Resource Files and Devices 17. Working with Plain Text, XML, and JSON Text Files 18. Virtual Reality and Extra Features 19. Automated Testing 20. Bonus Chapters 21. Other Books You May Enjoy

Displaying a digital countdown timer

This recipe will show you how to display a digital countdown clock, as shown here:

Getting ready

This recipe adapts the previous one. So, make a copy of the project for the previous recipe, and work on this copy.

For this recipe, we have prepared the script that you need in a folder named _Scripts in the 01_03 folder.

How to do it...

To create a digital countdown timer, follow these steps:

  1. Import the provided _Scripts folder.
  2. In the Inspector panel, remove the scripted component, ClockDigital, from the Text-clock GameObject.
  3. In the Inspector panel, add an instance of the CountdownTimer script class as a component by clicking the Add Component button, selecting Scripts, and choosing the CountdownTimer script class.
  1. Create a DigitalCountdown C# script class that contains the following code, and add an instance as a scripted component to the Text-clock GameObject:
using UnityEngine; 
using UnityEngine.UI; 

public class DigitalCountdown : MonoBehaviour { 
   private Text textClock; 
   private CountdownTimer countdownTimer; 

   void Awake() { 
         textClock = GetComponent<Text>(); 
         countdownTimer = GetComponent<CountdownTimer>(); 
   } 
void Start() { countdownTimer.ResetTimer( 30 ); } void Update () { int timeRemaining = countdownTimer.GetSecondsRemaining(); string message = TimerMessage(timeRemaining); textClock.text = message; } private string TimerMessage(int secondsLeft) { if (secondsLeft <= 0){ return "countdown has finished"; } else { return "Countdown seconds remaining = " + secondsLeft; } } }
  1. When you run the Scene, you will now see a digital clock counting down from 30. When the countdown reaches zero, the message countdown has finished will be displayed.
Automatically add components with [RequireComponent(...)]

The DigitalCountdown script class requires the same GameObject to also have an instance of the CountdownTimer script class. Rather than having to manually attach an instance of a require script, you can use the [RequireComponent(...)] C# attribute immediately before the class declaration statement. This will result in Unity automatically attaching an instance of the required script class.

For example, by writing the following, Unity will add an instance of CountdownTimer as soon as an instance of the DigitalCountdown script class has been added as a component of a GameObject:

using UnityEngine;   
using UnityEngine.UI;   

[RequireComponent (typeof (CountdownTimer))]   
public class DigitalCountdown : MonoBehaviour {   
Learn more from the Unity documentation at https://docs.unity3d.com/ScriptReference/RequireComponent.html.

How it works...

You have added instances of the DigitalCountdown and CountdownTimer C# script classes to your scene's UI Text GameObject.

The Awake() method caches references to the Text and CountdownTimer components in the countdownTimer and textClock variables. The textClock variable will be a reference to the UI Text component, whose text content we wish to update in each frame with a time-remaining message (or a timer-complete message).

The Start() method calls the countdown timer object's CountdownTimerReset(...) method, passing an initial value of 30 seconds.

The Update() method is executed in every frame. This method retrieves the countdown timer seconds remaining and stores this value as an integer (whole number) in the timeRemaining variable. This value is passed as a parameter to the TimerMessage() method, and the resulting message is stored in the string (text) variable message. This method finally updates the text property (that is, the letters and numbers that the user sees) of the textClock UI Text GameObject to equal to the string message about the remaining seconds.

The TimerMessage() method takes an integer as input, and if the value is zero or less, a message stating the timer has finished is returned. Otherwise (if greater than zero seconds remain) a message stating the number of remaining seconds is returned.

You have been reading a chapter from
Unity 2018 Cookbook - Third Edition
Published in: Aug 2018
Publisher:
ISBN-13: 9781788471909
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image