Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Unity UI Cookbook
Unity UI Cookbook

Unity UI Cookbook: Over 60 recipes to help you create professional and exquisite UIs to make your games more immersive

eBook
$29.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Unity UI Cookbook

Chapter 2. Implementing Counters and Health Bars

In this chapter, we will cover the following topics:

  • Implementing a score counter
  • Implementing lives counter
  • Creating a modular coin counter
  • Creating a symbolic lives counter
  • Implementing a linear health bar
  • Implementing a radial health bar
  • Creating a health bar with armor
  • Using multiple bars to make a multibar
  • Developing a kingdom hearts health bar style

Introduction

This chapter explains how to implement counters for keeping track of variables such as score, coins, and lives, and how to display them through texts or through more complex things such as health bars.

We will start by creating a counter for our score and we will proceed with more complex counters.

Later, we will introduce the concept of health bars and we will also learn how to change their look, so that they are more linear or radial.

Finally, in the last three recipes, we are going to implement very special and complex health bars, which are suitable for bosses, for example.

Implementing a score counter

In this recipe, we are going to create a score counter that displays the score as a number on the screen. To achieve this, we will use the Text (Script) component and create a script to handle it. Different functions will be available from this script, that will be called from other scripts to increase the score.

How to do it...

  1. First of all, we need to create new UI text to display our score. Thus, right-click on the Hierarchy panel and then UI | Text. Finally, rename it to ScoreCounter.

    Tip

    To rename an object, right-click on it and select Rename. Otherwise, we can change its name in the Inspector window.

  2. Next, we can change the Font and the Font Size in the Inspector as we wish, to better suit our needs.
  3. Furthermore, we should also adjust the Color. In this example, we will set it to white in order to be easily visible within the scene.
  4. Now, we need to drag the ScoreCounter with the Rect Tool (the hotkey for this tool is T, and we have seen it in the previous chapter...

Implementing a lives counter

This recipe teaches us how to use a Text (Script) component, along with a script, to create a counter. It is similar to the counter in the previous recipe; however, instead of keeping track of the score, here we are managing the number of lives that the player has.

How to do it...

  1. Like the first step in the previous recipe, we need to create a new UI text to display the number of lives. Right-click on the Hierarchy panel and then go to UI | Text. Finally, rename it to LivesCounter.
  2. We can also adjust the appearance, as we have done before with the ScoreCounter, so change the Font and the Font size. We can also set the Color of the text to white, and, finally, we place it in the scene through the Rect Tool, and so on.
  3. Ensure again that the Rich Text variable is checked in order to use styling tags.
  4. Next, let's create the script that manages the number of lives: click on Add Component | New Script in the Inspector, name it LivesCounterScript and then press Create...

Creating a modular coin counter

This recipe teaches us how to use a Text (Script) and Image (Script) components along with a script to create a counter with an icon. It is similar to the counters in the two previous recipes, but, instead, to keep track of the score or lives, here we manage the number of coins possessed by the player. In contrast to the first counter that doesn't have an upper bound, or like the second counter that has a maximum for lives, here, when the player has reached a certain number of coins, something will happen. For example, a life will be added and the counter starts from zero, creating modularity.

How to do it...

  1. As for the first step of the previous recipe, we need to create a new UI text to show the number of lives. Hence, right-click on the Hierarchy panel and then UI | Text. Finally, rename it to Modular Coin Counter.
  2. Let's adjust the settings to suit our needs, such as Font, Font Size, and Color, and also write in the Text variable 0 (zero).
  3. In order...

Creating a symbolic lives counter

This recipe teaches us how to use multiple Image (Script) components inside a script in order to create a symbolic lives counter. The number of lives are not displayed as a number, but with heart symbols on the screen. It is similar to the counter in the Implementing a lives counter recipe, but the logic to manage different icons is different.

How to do it...

  1. To begin, let's create a new empty game object. To do this, right-click on the Canvas object, since we want it as parent, and then Create Empty. Finally, rename it as SymbolicLivesCounter.

    Tip

    If there isn't the Canvas object in the scene, for example, in new scenes, we can create it by right-clicking on Hierarchy panel and then go to UI | Canvas.

  2. Next, click on SymbolicLivesCounter and add a new image by selecting UI | Image, and then rename the object just created as Heart1.
  3. Take a heart icon image, or create on our own, and import it in to our project. If our project is not set as 2D, remember...

Implementing a linear health bar

In this recipe, we are going to create a linear health bar. The health of the player will be displayed as a bar that shortens when the player's health decreases, and extends with the player's health increases. To achieve this, we will use the Image (Script) component in a different way than what we have in previous recipes, and develop a script to manage the length of the bar.

How to do it...

  1. First of all, we need to create our health bar, so let's open a graphic program and create it. We can create something that should looks like the following:
    How to do it...
  2. Then, import it in to our project. If the project isn't set to 2D, remember to set the Texture Type of the imported image to Sprite (2D and UI), and then click on Apply.
  3. Next, create a new Image. Right-click on the Hierarchy panel and then UI | Image, and rename it to Linear Healthbar.
  4. Inside the Image (Script) component, we have to change Image Type into Filled. The component should change a little...

Introduction


This chapter explains how to implement counters for keeping track of variables such as score, coins, and lives, and how to display them through texts or through more complex things such as health bars.

We will start by creating a counter for our score and we will proceed with more complex counters.

Later, we will introduce the concept of health bars and we will also learn how to change their look, so that they are more linear or radial.

Finally, in the last three recipes, we are going to implement very special and complex health bars, which are suitable for bosses, for example.

Implementing a score counter


In this recipe, we are going to create a score counter that displays the score as a number on the screen. To achieve this, we will use the Text (Script) component and create a script to handle it. Different functions will be available from this script, that will be called from other scripts to increase the score.

How to do it...

  1. First of all, we need to create new UI text to display our score. Thus, right-click on the Hierarchy panel and then UI | Text. Finally, rename it to ScoreCounter.

    Tip

    To rename an object, right-click on it and select Rename. Otherwise, we can change its name in the Inspector window.

  2. Next, we can change the Font and the Font Size in the Inspector as we wish, to better suit our needs.

  3. Furthermore, we should also adjust the Color. In this example, we will set it to white in order to be easily visible within the scene.

  4. Now, we need to drag the ScoreCounter with the Rect Tool (the hotkey for this tool is T, and we have seen it in the previous chapter...

Implementing a lives counter


This recipe teaches us how to use a Text (Script) component, along with a script, to create a counter. It is similar to the counter in the previous recipe; however, instead of keeping track of the score, here we are managing the number of lives that the player has.

How to do it...

  1. Like the first step in the previous recipe, we need to create a new UI text to display the number of lives. Right-click on the Hierarchy panel and then go to UI | Text. Finally, rename it to LivesCounter.

  2. We can also adjust the appearance, as we have done before with the ScoreCounter, so change the Font and the Font size. We can also set the Color of the text to white, and, finally, we place it in the scene through the Rect Tool, and so on.

  3. Ensure again that the Rich Text variable is checked in order to use styling tags.

  4. Next, let's create the script that manages the number of lives: click on Add Component | New Script in the Inspector, name it LivesCounterScript and then press Create and...

Creating a modular coin counter


This recipe teaches us how to use a Text (Script) and Image (Script) components along with a script to create a counter with an icon. It is similar to the counters in the two previous recipes, but, instead, to keep track of the score or lives, here we manage the number of coins possessed by the player. In contrast to the first counter that doesn't have an upper bound, or like the second counter that has a maximum for lives, here, when the player has reached a certain number of coins, something will happen. For example, a life will be added and the counter starts from zero, creating modularity.

How to do it...

  1. As for the first step of the previous recipe, we need to create a new UI text to show the number of lives. Hence, right-click on the Hierarchy panel and then UI | Text. Finally, rename it to Modular Coin Counter.

  2. Let's adjust the settings to suit our needs, such as Font, Font Size, and Color, and also write in the Text variable 0 (zero).

  3. In order to add an...

Left arrow icon Right arrow icon

Key benefits

  • Design and develop interactive and professional user interfaces (UIs) for games in Unity
  • Discover how to implement and deal with various in-game UI elements that will impress your players
  • This practical recipe guide will help you to efficiently create powerful and remarkable UIs using C# code

Description

With the increasing interest in game development, it's essential to design and implement a UI that reflects the game settings and shows the right information to the player. The Unity system is used to create complex and aesthetically pleasing user interfaces in order to give a professional look and feel to a game. Although the new Unity UI system is powerful and quite easy to use, by integrating it with C# scripts, it's possible to realize the potential of this system and bring an impressive UI to games. This guide is an invaluable collection of recipes if you are planning to use Unity to develop a game. Starting with the basic concepts of the UI components, we’ll take you all the way through to creating complex interfaces by including animations and dynamics elements. Based on real-world problems, these recipes will start by showing you how to make common UI elements such as counters and healthbars. You will then get a walkthrough of how to manage time using timers, and will learn how to format them. You will move on to decorating and animating the UI elements to vivify them and give them a professional touch. Furthermore, you will be guided into the 3D UI world and into HUD scripting. Finally, you will discover how to implement complex minimaps in the interface.

Who is this book for?

If you are a game developer with some experience in Unity and C# and want to create the best interactive experience fast and intuitively, then this book is for you. If you are an intermediate game developer or an expert, these recipes will help you bring out the power of the new UI Unity system.

What you will learn

  • Implement different kinds of counters and healthbars
  • Deal with timers and find out how to format them
  • Animate and vivify UI elements
  • Handle runtime customizations
  • Add complex Head-up displays (HUDs)
  • Design and implement 3D UIs
  • Integrate minimaps in the UI

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Dec 29, 2015
Length: 284 pages
Edition : 1st
Language : English
ISBN-13 : 9781785882005
Vendor :
Unity Technologies
Languages :
Concepts :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Dec 29, 2015
Length: 284 pages
Edition : 1st
Language : English
ISBN-13 : 9781785882005
Vendor :
Unity Technologies
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 170.97
Unity 5.x Cookbook
$60.99
Unity UI Cookbook
$54.99
Unity 5.x Shaders and Effects Cookbook
$54.99
Total $ 170.97 Stars icon

Table of Contents

11 Chapters
1. UI Essentials Chevron down icon Chevron up icon
2. Implementing Counters and Health Bars Chevron down icon Chevron up icon
3. Implementing Timers Chevron down icon Chevron up icon
4. Creating Panels for Menus Chevron down icon Chevron up icon
5. Decorating the UI Chevron down icon Chevron up icon
6. Animating the UI Chevron down icon Chevron up icon
7. Applying Runtime Customizations Chevron down icon Chevron up icon
8. Implementing Advance HUDs Chevron down icon Chevron up icon
9. Diving into 3D UIs Chevron down icon Chevron up icon
10. Creating Minimaps Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5
(13 Ratings)
5 star 69.2%
4 star 23.1%
3 star 0%
2 star 0%
1 star 7.7%
Filter icon Filter
Top Reviews

Filter reviews by




Francis Jan 12, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a good reference for anyone working on a game in Unity 3D that requires a good user interface. The book is organized to provide 60 recipes, which are code snippets and instructions to create various user interface elements. This makes it possible to read the book in any order so you can just get to the parts you are currently interested in.If you are working on a game and decide you need a Health Bar for one of your game characters you can just skip to Chapter 2 read a few pages that are illustrated well showing various types of health bars and associated code and then follow the step by step "How to do it..." instructions.Amazon lets you look inside the book so do that to see a recipe you might be interested in and the style of the author.This book covered all the types of user interfaces that I was interested in and the information in Chapter 8 about "Creating a directional radar" gave me some new ideas for a game that I am working on. The recipes in this book easily saved me hours of development time that I could have wasted trying to implement the same thing that is documented well in this book.
Amazon Verified review Amazon
Bart Knowles Jan 08, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I've been fumbling with Unity for many personal projects in recent years, now I'm trying to get more serious and turn it into a professional advantage. This cookbook comes as a very welcome bridge from my amateur days to (hopefully) better professional opportunities.It is extremely useful to be taken by the hand and guided step by step through implementing different elements of user interfaces. Boxes with "How to do it" and "How it works" clarify the most important issues at a glance.This is an extremely useful book. I read the electronic version and I am thinking of buying the paper version: it is more expensive but might turn out to be more wieldy in the long run.
Amazon Verified review Amazon
Jan Dyrda Jan 07, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I must tell you that this book... well, it delivers what is promised and give you even more.Pros:- It's written in very understandable way;- There's print-screen for almost every operation which means you'll need to be trying really hard to not understand what's written in Unity UI Cookbook;- Book contains tons of valuable tips and tricks. Some things you probably know already if you were developing anything in Unity already, but some tips were new for me.- If you know nothing about Unity UI and you can't decide what to choose... My choice would be this book;- Also: all code (working) is available for download which is nice touch for those who don't really like copying and pasting ;)What you'll learn from this book:General information about UI EssentialsHow to implement Counters and Health BarsHow to implement TimersHow to create Panels for MenusHow to decorate the UIHow to animate the UIHow to apply Runtime CustomizationsHow to implement Advance HUDsHow to dive into 3D UIsHow to create Minimaps... and that should be enough for the start. I have many Packt books and ebooks and this particular one was definitely worth money.
Amazon Verified review Amazon
Rakesh Patibanda Jan 09, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I have been a Unity freak for 4 years now and this book "Unity UI Cookbook" has the flavour, taste and spiciness for me to be able to call it a great dish. Are you are a novice or a pro user of Unity? Do not worry, you will find a recipe that suits your taste, always. I would highly recommend this book.
Amazon Verified review Amazon
Cristian Gradisteanu Jan 21, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Everything you need to know in order to be able to create beautiful and well designed user interfaces in Unity in once book. The book contains great information on how to create captivating and useful user interfaces (a must if you want to develop Unity games). It provides recipes that will help you please your players with engaging and modern interfaces. If you're considering developing Unity games...you can not skip this book!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.