Book Image

Unity 5.x Cookbook

Book Image

Unity 5.x Cookbook

Overview of this book

Unity 5 is a flexible and intuitive multiplatform game engine that is becoming the industry's de facto standard. Learn to craft your own 2D and 3D computer games by working through core concepts such as animation, audio, shaders, GUI, lights, cameras, and scripting to create your own games with one of the most important and popular engines in the industry. Completely re-written to cover the new features of Unity 5, this book is a great resource for all Unity game developers, from those who have recently started using Unity right up to game development experts. The first half of the book focuses on core concepts of 2D game design while the second half focuses on developing 3D game development skills. In the first half, you will discover the new GUI system, the new Audio Mixer, external files, and animating 2D characters in 2D game development. As you progress further, you will familiarize yourself with the new Standard Shaders, the Mecanim system, Cameras, and the new Lighting features to hone your skills towards building 3D games to perfection. Finally, you will learn non-player character control and explore Unity 5's extra features to enhance your 3D game development skills.
Table of Contents (20 chapters)
Unity 5.x Cookbook
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Game development is a broad and complex task. It is an interdisciplinary field, covering subjects as diverse as artificial intelligence, character animation, digital painting, and sound editing. All these areas of knowledge can materialize as the production of hundreds (or thousands!) of multimedia and data assets. A special software application—the game engine—is required to consolidate all of these assets into a single product.

Game engines are specialized pieces of software, which used to belong to an esoteric domain. They were expensive, inflexible, and extremely complicated to use. They were for big studios or hardcore programmers only. Then along came Unity.

Unity represents the true democratization of game development. It is an engine and multimedia editing environment that is user-friendly and versatile. It has free and Pro versions; the latter includes even more features. As we write this preface, Unity offers deployment to:

  • Mobile: Android, iOS, Windows Phone, and BlackBerry

  • Web: WebGL

  • Desktop: PC, Mac, and Linux platforms

  • Console: PS4, PS3, Xbox One, XBox 360, PlayStation Mobile, PlayStation Vita, and Wii U

  • Virtual Reality (VR)/Augmented Reality (AR): Oculus Rift and Gear VR

Today, Unity is used by a diverse community of developers all around the world. Some are students and hobbyists, but many are commercial organizations, ranging from garage developers to international studios, who use Unity to make a huge number of games — some you might have already played on one platform or another.

This book provides over 100 Unity game development recipes. Some recipes demonstrate Unity application techniques for multimedia features, including working with animations and using preinstalled package systems. Other recipes develop game components with C# scripts, ranging from working with data structures and data file manipulation, to artificial intelligence algorithms for computer-controlled characters.

If you want to develop quality games in an organized and straightforward way, and want to learn how to create useful game components and solve common problems, then both Unity and this book are for you.

What this book covers

Chapter 1, Core UI – Messages, Menus, Scores, and Timers, is filled with UI (User Interface) recipes to help you increase the entertainment and enjoyment value of your games through the quality of the interactive visual elements. You'll learn a wide range of UI techniques, including updatable text and images, directional radars, countdown timers, and custom mouse cursors.

Chapter 2, Inventory GUIs, shows you how many games involve the player-collecting items, such as keys to open doors, ammo for weapons, or choosing from a selection of items, such as from a collection of spells to cast. The recipes in this chapter offer a range of text and graphical solutions for displaying inventory status to the player, including whether they are carrying an item or not, or the maximum number of items they are able to collect.

Chapter 3, 2D Animation, includes powerful 2D animation and physics features. In this chapter, we present recipes to help you understand the relationships between the different animation elements in Unity, exploring both the movement of different parts of the body and the use of sprite-sheet image files that contain sequences of sprite frames pictures.

Chapter 4, Creating Maps and Materials, contains recipes that will give you a better understanding of how to use maps and materials with Unity 5's new Physically Based Shaders, whether you are a game artist or not. It is a great resource for exercising your image editing skills.

Chapter 5, Using Cameras, explains recipes covering techniques for controlling and enhancing your game's camera. This chapter will present interesting solutions to work with both single and multiple cameras.

Chapter 6, Lights and Effects, offers a hands-on approach to a number Unity's lighting system features, such as cookie textures, Reflection maps, Lightmaps, Light and Reflection probes, and Procedural Skyboxes. Also, it demonstrates the use of Projectors.

Chapter 7, Controlling 3D Animations, focuses on character animation, and demonstrates how to take advantage of Unity's animation system — Mecanim. It covers a range of subjects from basic character setup to procedural animation and ragdoll physics.

Chapter 8, Positions, Movement and Navigation for Character GameObjects, presents a range of directional recipes for computer-controlled objects and characters, which can lead to games with a richer and more exciting user experience. Examples of these recipes include spawn points, checkpoints, and waypoints. It also includes examples that make groups of objects flock together, and the use of Unity NavMeshes for automated path-finding over terrains and around obstacles.

Chapter 9, Playing and Manipulating Sounds, is dedicated to making sound effects and soundtrack music in your game more interesting. The chapter demonstrates how to manipulate sound during runtime through the use of scripts, Reverb Zones, and Unity's new Audio Mixer.

Chapter 10, Working with External Resource Files and Devices, throws light on how external data can enhance your game in ways such as adding renewable content and communicating with websites. The chapter also includes recipes on automating your builds with Unity Cloud, and how to structure your projects, so they can be easily backed up using online version control systems such as GitHub.

Chapter 11, Improving Games with Extra Features and Optimization, provides several recipes with ideas for adding extra features to your game (such as adding slow motion and securing online games). Many other recipes in this chapter provide examples of how to investigate and potentially improve the efficiency and performance of your game's code.

Chapter 12, Editor Extensions, provides several recipes for enhancing design-time work in the Unity Editor. Editor Extensions are scripting and multimedia components, that allows working with custom text, UI presentation of the game parameters, data in the Inspector and Scene panels, and custom menus and menu items. These can facilitate workflow improvements, thus allowing game developers to achieve their goals quicker and easier.

What you need for this book

All you need is a copy of Unity 5.x, which can be downloaded for free from http://www.unity3d.com.

If you wish to create your own image files for the recipes in Chapter 4, Creating Maps and Materials, you will also need an image editor, such as Adobe Photoshop, which can be found at http://www.photoshop.com, or GIMP, which is free and can be found at http://www.gimp.org.

Who this book is for

This book is for anyone who wants to explore a wide range of Unity scripting and multimedia features, and find ready-to-use solutions for many game features. Programmers can explore multimedia features, and multimedia developers can try their hand at scripting.

From intermediate to advanced users, from artists to coders, this book is for you, and everyone on your team!

It is intended for everyone who has the basics of using Unity, and a little programming knowledge in C#.

Sections

In this book, you will find several headings that appear frequently.

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software, or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, folder names, filenames, file extensions, pathnames, and user input are shown as follows: " For this recipe, we have prepared the font that you need in a folder named Fonts in the 1362_01_01 folder."

URLs are shown as follows: Learn more about the Unity UI on their manual pages at http://docs.unity3d.com/Manual/UISystem.html.

A block of code is set as follows:

  void Start (){
    textClock = GetComponent<Text>();
  }

  void Update (){
    DateTime time = DateTime.Now;
    string hour = LeadingZero( time.Hour );
    string minute = LeadingZero( time.Minute );
    string second = LeadingZero( time.Second );

    textClock.text = hour + ":" + minute + ":" + second;
  }

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "In the Hierarchy panel, add a UI | Text GameObject to the scene – choose menu: GameObject | UI | Text."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example codes and color images

All the files you need to complete the recipes in the book can be downloaded from: https://github.com/dr-matt-smith/unity-5-cookbook-codes.

The downloadable codes are fully commented, and completed Unity projects for each recipe are also provided. In addition you'll also find a folder containing the color images for each chapter in this repository.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.