Book Image

Learning Unity 2D Game Development by Example

By : Venita Pereira
Book Image

Learning Unity 2D Game Development by Example

By: Venita Pereira

Overview of this book

<p>If you are looking for a guide to create 2D games using Unity, look no further. With this book, you will learn all the essentials of 2D game development by creating five real-world games in a step-by-step manner throughout the course of this book.</p> <p>Starting with a blank scene, you will learn all about the new Unity 2D toolset, which will enable you to bring your scene to life. You will create characters, make them move, create some enemies, and then write code to destroy them. After figuring out all the necessities of creating a game, this book will then assist you in making several different games: games with collision, parallax scrolling, Box2D, and more.</p> <p>By the end of this book, you will not only have created several small games, but you will also have the opportunity to put all your new-found knowledge into creating and deploying a larger, full game.</p>
Table of Contents (17 chapters)
Learning Unity 2D Game Development by Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Buttons


Usually, the very first input that is required from a player in most games is from buttons on the main menu of a game. Thus, we are now going to create our own buttons using the OnGui function provided by Unity.

OnGui

The OnGUI function is used for handling GUI events, the creation and the look and functionality of the game's GUI. It is an event function that is part of the well-defined set of callbacks that Unity provides, so it gets called automatically like Start() and Update(). We, therefore, do not call it within another function.

OnGUI can be called several times per frame depending on its implementation. It will get called once per GUI event.

GUILayout.Button

We will use the existing Unity class GUILayout and its function Button to create our buttons. We specify the text that we would like to display in our buttons as well as our buttons' dimensions as parameters within the function as shown in the following script:

function OnGUI()
{

Note

If we click on Button 1 as input, then we...