Book Image

Mastering Unity 2D game development

By : Simon Jackson
Book Image

Mastering Unity 2D game development

By: Simon Jackson

Overview of this book

Table of Contents (21 chapters)
Mastering Unity 2D Game Development
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Overview
Index

Preface

Unity3D has long been viewed as a massive 3D game-making middleware system, with lots of power and an easy-to-use editor. Now, with 2D games back in fashion, Unity has created a 2D toolset for developers with the know-how to create great, customized games.

If you are looking for a book that will show you how to make a fully functional, customizable game product with popular game functionality, then this is the book for you. You will learn how to build an RPG game framework, learning lots of tips and tricks along the way, from advanced C# scripting to getting the most out of Unity's built-in features such as Mecanim and curves, but in ways you may have not even considered.

While creating your own character with its very own little village, you will come to learn about all the new 2D features and how to make the most out of them. Then, you will dive into the big wild world with your character, discovering how to manage different types of scenes, scripting random events, and the dreaded encountering of enemies. You will learn how to make your character ready for battles (with a little shopping) and engaging hordes of angry creatures just rumbling for a fight; how they react is completely up to you.

The one thing this title doesn't cover is audio, as this hasn't changed in Unity 4.3. With so much preparation for Unity 5, it's fair to say that big things are coming; however, there is more than enough to really sink your teeth into.

By the end of this book, you will be able to architect, create, deploy, and integrate your game with all of your intended platforms, and you'll also have the knowledge to build and customize the Unity editor and the games you create with confidence. You will also be schooled with tricks of the trade on marketing and monetization, as well as targeting as many platforms as possible, with a keen focus on how to best profit from your title.

The lessons you will learn in this book will also set you in a good stead for Unity 5 as everything has been checked in the latest beta's. In fact, there is only one slight code change required at the time of writing (which is highlighted in Chapter 9, Getting Ready to Fight).

The screens may change slightly, but it all just works. This is both a testament to Unity 5's ability to upgrade projects and that the 2D system is rock solid; everything you learn now will be valid for Unity 5.

No goblins were hurt during the production of this title; however, a few were extremely grumpy about their poor working conditions.

What this book covers

Chapter 1, Overview, starts with a look at what Unity has brought to the table from Version 4.2. In this chapter, we will have a walkthrough of the new 2D system and other new features.

Chapter 2, Character Building, involves rolling up our sleeves as we dig in and start working with 2D assets and sprites, thereby uncovering the sprite editor and a host of other interesting features.

Chapter 3, Getting Animated, introduces that animation is a key in any 2D title; you need more than just a picture to tell a story, so we delve into the new and improved Unity animation system and dope sheet. Beware, curves ahead.

Chapter 4, The Game World, explains that with the basics in hand, we will build our home town and let our character run free within it.

Chapter 5, NPCs and Interactions, explains that an RPG game without people to talk to would be kind of dull. Here, we will build messaging and conversation systems, readying ourselves to leave the nest and venture beyond. Advanced coding, engage.

Chapter 6, The Big Wild World, widens the scope of what we can see, and discusses art and assets, building a map view for the player to navigate in. If you look closely, you can just about see your house from here.

Chapter 7, Encountering Enemies and Running Away, discusses that the world is a big and scary place; you stumble upon a crowd of goblins snacking on their latest meal (who knows what's in that pot), and then get scared and run away. Here, we go through building a battle scene, including setting up Mecanim as a state and AI machine.

Chapter 8, Shopping for Weapons, dives into lots of shiny things! Leveraging our 2D skills, we build a simple shop scene, reusing our messaging system to add interactivity and expand the player's structure adding an inventory. Plus, we look at the other graphical ways of displaying the player's inventory with a cool command bar implementation.

Chapter 9, Getting Ready to Fight, makes us ready to rumble! We have a battle scene but no battle engine yet. Now, it's time to expand on what we have created with a turn-based battle engine, flexing Mecanim's muscles in ways you probably haven't considered.

Chapter 10, The Battle Begins, depicts us opening fire (or at least the axe or sword we came with) on the unsuspecting goblins and shattering their tiny bodies with a gratuitous blood scene (PEGI rating pending). From here, it's up to you how you wish to proceed.

Chapter 11, Onward Wary Traveler, describes that we have a game framework—all it needs is content and some packaging. So, we'll look at what's involved in finishing your game. Not stopping there, we will look at how you can extend the editor to tame this wild beast to work for us and make it build our content for us (or at the very least, make it a whole lot easier).

Chapter 12, Deployment and Beyond, covers the time taken to tackle the last piece of the puzzle, putting your game on a device. We walk through what it means to be multiplatform and build a trustworthy save/load system that will work on multiple platforms, not just one. We finish with a handy marketing section, aimed to arm you and help make your final product successful in the marketplace.

Appendix, Additional Resources, has a heap of assets, links, and information resources to help you with your game building travels.

What you need for this book

In order to follow this book, you will need the Unity 3D software available at http://unity3d.com/unity/download.

You can use any version of Unity from Version 4.3, but I recommend the latest 4.x version, which at the time of writing was Version 4.5 (all screenshots have been updated to this version).

You must be familiar with Unity's basic workflow: the words GameObject, components, and Editor/Inspector should be familiar to you.

All the code pertaining to coding skills are available here and explained and commented where appropriate. So, if you are not familiar with them, you will still be able to understand them.

While working with this book, we will be using several freely available assets from the Web, plus a few I cobbled together myself (using my poor programmer art skills). All of these are available as a separate package download along with the book's code downloads mentioned further.

Who this book is for

Whether you are a beginner starting to work with Unity 3D, an intermediate, or a professional developer looking to make use of the new 2D features of Unity, this book is for you.

The book also covers some intermediate and advanced coding topics, which are explained for developers of any level such that they are easy to follow.

Conventions

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

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Using a path-like name such as playerScene1BounceToWallScript."

A block of code is set as follows:

void OnSceneGUI()
{
  CameraLookAt targetScript = (CameraLookAt)target;
  targetScript.cameraTarget = Handles.PositionHandle(targetScript.cameraTarget, Quaternion.identity);
  if (GUI.changed)
  EditorUtility.SetDirty(target);
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are highlighted:

void OnSceneGUI()
{
  CameraLookAt targetScript = (CameraLookAt)target;

  targetScript.cameraTarget = Handles.PositionHandle(targetScript.cameraTarget, Quaternion.identity);
  Handles.SphereCap(0, targetScript.cameraTarget, Quaternion.identity, 2);
  if (GUI.changed)
  EditorUtility.SetDirty(target);
}

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "You can easily change the profile that the editor is using at any time by navigating to Edit | Project Settings| Editor and changing the Default Behavior Mode option."

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 may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via 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 on 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 code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Additionally, the author has provided a support forum for the book. This forum provides direct support from the author on your queries and any forthcoming announcements regarding the title. You can find this forum at http://bit.ly/MasteringUnity2DForums.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: https://www.packtpub.com/sites/default/files/downloads/7347OT_Graphics.pdf.

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 would 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright 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

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.