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
$43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781785885822
Vendor :
Unity Technologies
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Dec 29, 2015
Length: 284 pages
Edition : 1st
Language : English
ISBN-13 : 9781785885822
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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.