Book Image

Unity 2D Game Development Cookbook

By : Claudio Scolastici
Book Image

Unity 2D Game Development Cookbook

By: Claudio Scolastici

Overview of this book

<p>Unity is a powerful game development engine that provides rich functionalities to create 2D and 3D games.</p> <p>Unity 2D Game Development Cookbook is a practical guide to creating games with Unity. The book aims to serve the purpose of exploring problematic concepts in Unity for 2D game development, offering over 50 recipes that are easy to understand and to implement, thanks to the step-by-step explanations and the custom assets provided. The practical recipes provided in the book show clearly and concisely how to do things right in Unity. By the end of this book, you'll be near "experts" when dealing with Unity. You will also understand how to resolve issues and be able to comfortably offer solutions for 2D game development.</p>
Table of Contents (15 chapters)
Unity 2D Game Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using OnGUI() to display game data


As we are gathering game data about the player's performance, we can display that information on screen to the player's advantage.

Unity offers a collection of functions and constructs to display game data on the screen and create the Graphic User Interface (GUI) of your game. OnGUI() is an important method available in Unity and used to create and control GUI elements to be displayed on the screen. Inside the function, it is possible to put lines of code that create interface controls such as text fields, buttons, and sliders at runtime. We advise you to check out the following link:

http://docs.unity3d.com/Manual/gui-Basics.html

In this recipe, we will use the OnGUI() function to display game data. We will create a script to access the number of lives and the items collected, and display them on the screen.

We need a couple of screenshots to be used as icons too, so get ready to follow our instructions.

Getting ready

The first step is to import the images to...