Getting Started with Unity 5.x 2D Game Development
()
About this ebook
- Build a complete and exciting 2D Tower Defense game from scratch.
- Understand and learn to perform each phase of the game development pipeline
- Homework and exercises to improve your skills and take them to the next level
If you are looking forward to get started with 2D game development, either if you are a newcomer to this world, or you came from 3D games or other game engines, this book is for you. Although there are many references to other resources throughout the book, it is assumed that you have a general understanding of C# and its syntax and structure.
Related to Getting Started with Unity 5.x 2D Game Development
Related ebooks
Unity 4.x Game Development by Example Beginner's Guide Rating: 3 out of 5 stars3/5Extending Unity with Editor Scripting Rating: 0 out of 5 stars0 ratingsCorona SDK Mobile Game Development Beginner's Guide Rating: 5 out of 5 stars5/5Unity Game Development Scripting Rating: 0 out of 5 stars0 ratingsMastering Unity 2D Game Development - Second Edition Rating: 0 out of 5 stars0 ratingsLearning LibGDX Game Development - Second Edition Rating: 0 out of 5 stars0 ratingsCRYENGINE Game Development Blueprints Rating: 0 out of 5 stars0 ratingsMastering Unity 5.x Rating: 0 out of 5 stars0 ratingsCryENGINE 3 Game Development Beginner's Guide Rating: 0 out of 5 stars0 ratingsUnity Game Development Essentials Rating: 5 out of 5 stars5/5Unity 5.x Cookbook Rating: 0 out of 5 stars0 ratingsUnity 5 for Android Essentials Rating: 4 out of 5 stars4/5Unreal Development Kit Game Design Cookbook Rating: 0 out of 5 stars0 ratingsUnity Android Game Development by Example Beginner's Guide Rating: 4 out of 5 stars4/5HTML5 Game Development with GameMaker Rating: 0 out of 5 stars0 ratingsUnreal Engine Physics Essentials Rating: 1 out of 5 stars1/5Getting Started with Unity 5 Rating: 5 out of 5 stars5/5Building an RPG with Unreal Rating: 0 out of 5 stars0 ratingsUnity Multiplayer Games Rating: 5 out of 5 stars5/5Unity Character Animation with Mecanim Rating: 0 out of 5 stars0 ratingsGameMaker Cookbook Rating: 0 out of 5 stars0 ratingsUnity 3.x Game Development by Example Beginner's Guide Rating: 3 out of 5 stars3/5Mastering LibGDX Game Development Rating: 0 out of 5 stars0 ratingsMastering Unity Scripting Rating: 0 out of 5 stars0 ratingsSFML Blueprints Rating: 0 out of 5 stars0 ratingsUnity Game Development Blueprints Rating: 0 out of 5 stars0 ratingsAugmented Reality Game Development Rating: 0 out of 5 stars0 ratingsMastering Oculus Rift Development Rating: 0 out of 5 stars0 ratingsHaxe Game Development Essentials Rating: 0 out of 5 stars0 ratingsLibgdx Cross-platform Game Development Cookbook Rating: 0 out of 5 stars0 ratings
Programming For You
Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5C++ Learn in 24 Hours Rating: 0 out of 5 stars0 ratingsSQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratingsOneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsSQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Unreal Engine from Zero to Proficiency (Foundations): Unreal Engine from Zero to Proficiency, #1 Rating: 0 out of 5 stars0 ratingsLinux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Web Designer's Idea Book, Volume 4: Inspiration from the Best Web Design Trends, Themes and Styles Rating: 4 out of 5 stars4/5
Reviews for Getting Started with Unity 5.x 2D Game Development
0 ratings0 reviews
Book preview
Getting Started with Unity 5.x 2D Game Development - Sapio Francesco
Table of Contents
Getting Started with Unity 5.x 2D Game Development
Credits
About the Author
Acknowledgment
About the Reviewer
www.PacktPub.com
Why subscribe?
Customer Feedback
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. A Flat World in Unity
Learning game development
Tower defense games
Designing our game
Getting ready
Remembering the past to build the future
Organizing the project
A 2D world
Sprites
Sprite Renderer
Importing new friends
The Sprite Editor
Manual mode
Automatic mode
Polygonal mode
Sprite Editor for UI-9-slice scaling
Preparing the assets
Scenes as levels
Setting proportions
There is more about the Unity interface
Homework
Summary
2. Baking Cupcake Towers
2D objects
Parenting game objects
Difference between world coordinates and local coordinates
Ordering the different layers with Z-Buffering
Unfolding the map
Layers and tags
Prefabs
The game view
Math background
Scripting in Unity
Creating new scripts
Basics about scripts
Variables
Attributes
Functions
Comments
Execution order
Making sprinkles
The projectile class
Scripting the projectile mother class
Tons of sprinkles through Prefabs
Baking cupcakes towers
What a cupcake tower does
Scripting the cupcake tower
Shooting to the pandas
Upgrading the cupcake tower, making it even tastier
A pre-baked cupcake tower through Prefabs
More about coding in Unity
Static variables
Inheritance
Random numbers in Unity
Homework
Summary
3. Communicating with the Player – the User Interface
Getting ready
Designing the user interface
Programming the user interface
User interface system
Canvas
Screen space – Overlay
Screen space – Camera
World space
Draw order of UI elements
Visual components
The Image component
The Text component
Basic transformations
The Rect Tool
The Rect Transform
Layout components
Fitters
Layout groups
The layout element component
Interaction components
The selectable base class
Interactable option
Transition options
Navigation options
Button
Toggle and Toggle Group
Slider
Scrollbar
Dropdown
Input Field
Scroll Rect
More about UI rendering
The canvas renderer
More visual components
UI effect components
UI and lights
The canvas components
Canvas group
Event system
Scripting user interfaces
Designing the interface for our game
Preparing the scene for the UI
Creating a health bar
Creating and placing the health bar
Scripting the health bar
Implementing the sugar meter
Creating and place
Scripting the sugar meter
More about UI scripting – handlers
What about all the rest?
Homework
Summary
4. No Longer Alone – Sweet-Toothed Pandas Strike
Getting ready
Animations
A historical overview
Animations in video games
Workflow for animations
Animations clips and the Animator component
Creating Animation clips with a controller
The Animator component
Creating the other animation clips
The Animator
The Animator window
The Animator state machine
The Animator states
Special states
The animator parameters
The animator transitions
Transition settings
Transition graph
Transition conditions
Testing transitions
The panda's Animation State Machine
Testing the panda's Animation State Machine
Scripting Animations
State machine behaviours
The destroy behaviour
The panda script
More about Animations
Avatars
Sub-state machines
The hierarchical location menu
Layers in the Animator
Blending trees
Animator Override Controller
Culling Mode in the Animator component
Root motion
Inverse Kinematic
Animator component information box
Legacy animation
Get animated!!
Homework
Summary
5. The Secret Ingredient Is a Dash of Physics
Getting ready
Physics in video games
Physics – basics
World coordinates and local coordinates
Velocity
Mass
Centre of mass
Kinematics
Dynamics
Force and torque
Collisions
Rigid body
Friction – linear and angular drag
The Unity Physics engine
Understanding Physics in Unity
Physics settings in Unity
Physics components
Rigidbodies
How a Rigidbody 2D works
Body types
Rigidbody properties
Dealing with rigidbodies
Colliders
Dealing with colliders
Joints
Main properties of Joints
The other rigidbody
Breaking joints
Specific joints
Distance Joint 2D
Fixed Joint 2D
Friction Joint 2D
Hinge Joint 2D
Relative Joint 2D
Slider Joint 2D
Spring Joint 2D
Target Joint 2D
Wheel Joint 2D
Effectors
Constant Force 2D
Area Effector 2D
Buoyancy Effector 2D
Point Effector 2D
Platform Effector 2D
Surface Effector 2D
Physics Material 2D
Dealing with physics in Unity
Other things about Physics
The Simulate setting on rigidbodies
Physics Raycaster 2D component
The other Physics settings
Gizmos for colliders
Physics for our game
Set up Pandas as a rigidbodies
Set up projectiles as rigidbodies
Detect sprinkles
Homework
Summary
6. Through a Sea of Sprinkles – Navigation in Artificial Intelligence
Getting ready
Introduction to artificial intelligence
The importance of artificial intelligence in video games
Navigation
Aspects of navigation
Pathfinding and its techniques
Waypoints for enemies
Getting the waypoint coordinates
Implementing waypoints – the first/static way
Implementing waypoints in the Game Manager
Moving along the designed path – static
Implementing waypoints – the second/dynamic way
Implementing waypoints as separate entities
Moving along the designed path – dynamic
More about artificial intelligence in games
Other techniques for navigation at the pathfinding level
Navigation at the level of steering behaviours
Navigation at the level of pathfinding/decision making – belief-driven pathfinding
Beyond navigation
Homework
Summary
7. Trading Cupcakes and the Ultimate Battle for the Cake – Gameplay Programming
Getting ready
What does gameplay programming mean?
Planning what is left to implement for our game
Trading cupcake towers
The trading parent class
Modifying the CupcakeTowerScript
Buying cupcake towers
Selling cupcake towers
Upgrading cupcakes towers
Adding the trading options to the user interface
Placing the towers
Sketching the idea of how it works
Allowed areas
Scripting the placement script
Selecting the towers
The Game Manager
Game over conditions
Game over feedback
The GameOver function
Keeping track of the game's progress
Panda invasion – spawning Pandas
What is a coroutine?
Sketching the idea of how it works
Setting up the spawning system
Managing waves
The single wave
The main menu
Designing the main menu
Creating the main menu in another scene
Loading scenes through scripts
Techniques we learnt in this chapter
Homework
Summary
8. What Is beyond the Cake?
Enhancing and improving your game
Improving cupcake towers
Shooting policies
Special sprinkles
Aging and pricing model
Improving the user interface
Improving levels
Multilevel
Large maps
Many paths
Many Pandas
Multiphase bosses
A better spawning system
Switching difficulty at runtime
Training and extending your Unity skills to become a better game developer
Making things easier for other team members
Exposing events
Sprinkles pooling
Saving your data
Debugging
Remote Logs
Cleaning the release version
More about communication between scripts
Documenting the code
Protecting your game
Building for more than one platform
Input/output devices
Virtual reality in Unity
Balancing the game
Extending the Unity editor
Multiplayer and networking
Practice makes perfect
Improving the atmosphere of the game
Visuals
Color schemes
Homework
Lighting
Lights in Unity
Homework
Environment
Homework
Special effects
Particle systems
Post processing
Other visual effects
Audio
Music
Sound effects
Eliciting emotions
Homework
Audio in Unity
External audio systems
Teamwork
It is not just about you, it is about working in a team
Sharing a common vision
Managing expectations
Collaboration and communication are key
Ways to communicate
Version control
Make a GDD and stick to it
Keeping it tidy with project management tools
Slack
HacknPlan
Drive
Dropbox
Trello
Redbooth
GitHub
BitBucket
Calendar
Hootsuite
Polishing your game
Processing the power
Checking the build size
Texture optimitation
Stats and profiling
Other optimization tips
Playtesting
Why are you even playtesting?
Exercise
Whom do you need to playtest?
The solo test run
Making it a social occasion
Putting it within boundaries
Reaching out to family and friends
Those who are strangers
Those who you want to play your game
When
Where
What
A little goes a long way
How
Methods of playtesting
Observe
Question and explain
Reflect and follow up
Creating your online presence
Do your research
Conducting an audit
Engaging with your audience
Rewarding engagement
Marketing on social media
Blog about it
Exercise
MailChimp
Getting ready to publish
Crowdfunding campaigns
Building in Unity
Clearing the air
Accepting terms and conditions
Localization
Ethical considerations
Summary
Final notes and goodbye
Getting Started with Unity 5.x 2D Game Development
Getting Started with Unity 5.x 2D Game Development
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: February 2017
Production reference: 1080217
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78439-717-3
www.packtpub.com
Credits
About the Author
Francesco Sapio obtained his computer science and controls engineering degree from Sapienza University of Rome, Italy, with a couple of semesters in advance, graduating summa cum laude; he is currently studying there for a master's of science and engineering degree in artificial intelligence and robotics.
He is a Unity 3D and Unreal expert, skilled game designer, and experienced user of major graphics programs. He developed Game@School (Sapienza University of Rome), an educational game for high-school students to learn concepts of physics, and the Sticker Book series (Dataware Games), a cross-platform series of games for kids. In addition, he worked as consultant for the (successfully funded by Kickstarter) game Prosperity – Italy 1434 (Entertainment Game Apps, Inc) and for the open online collaborative ideation system titled Innovoice (Sapienza University of Rome). Moreover, he has been involved in different research projects such as Belief-Driven Pathfinding (Sapienza University of Rome), which is a new technique of path finding in video games that was presented as a paper at the DiGRA-FDG Conference 2016; and perfekt.ID (Royal Melbourne Institute of Technology), which included developing a recommendation system for games.
Francesco is an active writer on the topic of game development. Recently, he authored the book Unity UI Cookbook, Packt Publishing. It teaches readers how to develop exciting and practical user interfaces for games within Unity, and he wrote a short e-guide, What do you need to know about Unity, Packt Publishing. In addition, he co-authored the book Unity 5.x 2D Game Development Blueprints, Packt Publishing, which has also been transformed in the video course Unity 5.x Game Development Projects, Pack Publishing. He has also been a reviewer for the following books: Game Physics Cookbook, Packt Publishing, Unity 5.x by Example, Packt Publishing, and Unity Game Development Scripting, Packt Publishing.
Francesco is also a musician and a composer, especially of soundtracks for short films and video games. For several years, he worked as an actor and dancer, where he was a guest of honor at the Teatro Brancaccio in Rome. In addition, he has volunteered as a children's entertainer at the Associazione Culturale Torraccia in Rome. Finally, Francesco loves math, philosophy, logic, and puzzle solving, but most of all, creating video games—thanks to his passion for game designing and programming.
You can contact him at www.francescosapio.com.
Acknowledgment
I'm deeply thankful to my parents for their infinite patience, enthusiasm, and support throughout my life. Moreover, I'm thankful to the rest of my family, in particular to my grandparents, since they have always encouraged me to do better in my life with the Latin expressions Ad maiora and Per aspera ad astra.
I also want to thank Lauren S. Ferro, the reviewer, for her fantastic feedback and help, which have enhanced the quality of this book. Another important person, without whom this book couldn't have seen the light, is Mayur Pawanikar, the content development editor, who supported me throughout the whole process.
Finally, a huge thank to all the special people around me whom I love. In particular to my girlfriend: I'm grateful for all of your help in everything, I love you.
About the Reviewer
Lauren S. Ferro is a gamification consultant and designer of game and game-like applications. She has worked, designed, consulted, and implemented strategies for a range of different purposes from professional development, recommendation systems, and educational games. She is an active researcher in the area of gamification, player profiling, and user-centered game design. Lauren runs workshops both for the general public and companies that focus on designing user-centered games and game-like applications. She has written a book about implementing gamification within Unity titled Gamification with Unity 5.x and is the developer of the game design resource Gamicards, which is a paper prototyping tool for both game and game-like experiences.
www.PacktPub.com
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thank you for purchasing this Packt book. We take our commitment to improving our content and products to meet your needs seriously—that's why your feedback is so valuable. Whatever your feelings about your purchase, please consider leaving a review on this book's Amazon page. Not only will this help us, more importantly it will also help others in the community to make an informed decision about the resources that they invest in to learn.
You can also review for us on a regular basis by joining our reviewers' club. If you're interested in joining, or would like to learn more about the benefits we offer, please contact us: customerreviews@packtpub.com.
Preface
When Packt asked me to write this book, we had in mind something more modest than the book that you have today. While I was writing, I realized how many books can be found on the shelves that teach the basics without giving any practical insight on how to use that knowledge to actually make a game. Therefore, I slightly deviated the course of this book. As a result, it has changed into a somewhat solid manual about Unity, with a particular focus on 2D game development. The book that you are holding in your hands (either hard copy or on your tablet) is the outcome of a lot of effort trying to create something that is easy for the readers to understand, without sacrificing completeness or practical use of the different tools learned. Thus, everything is explained in detail, along with many examples of usage.
Also, I felt that another big deficiency of many books is the lack of Homework sections that provide the reader with exercises to improve her/his skills. In fact, I believe that these sections are important, since they provide a common ground in case you want to challenge yourself and take the discussion beyond the book with your friends, course mates, or colleagues. In the field of game development, it's important to collaborate, because a game is often the result of the coordinate efforts of many talented and passionate people.
For this book, I decided to support the learning of the different tools we will face by building a tower defense game on the joyful and sugary theme of cupcakes, sprinkles, and sweet-toothed pandas who will try to bite the delicious player's cake. This project will guide the reader through the book, and help her/him to develop a practical skillset as well.
Whoever you are, a hungry student of knowledge, a professor looking for a book to adopt in your classes, an expert in game development trying out Unity (or willing to extend your understanding of Unity), or just a passionate hobbyist, I hope you will enjoy this book.
What this book covers
Chapter 1, A Flat World in Unity, is an introduction to the 2D world of Unity. You will discover how to set up your project, import assets, and make them ready to use. In particular, we will go through in detail on how to use the Sprite Editor.
Chapter 2, Baking Cupcake Towers, teaches you how to integrate code within your game. We will cover the important and fundamental concepts of Unity and we will see how to script game objects by starting to create the behaviors of our cupcake towers for the tower defense game we are building.
Chapter 3, Communicating with the Player – the User Interface, deals with the important task to provide feedback to your players through the use of user interfaces (UIs). You will learn how to design them by discovering the general principles behind them, and learn how to implement any UI within the UI system of Unity.
Chapter 4, No Longer Alone – Sweet-Toothed Pandas Strike, introduces us to the terrible sweet-toothed Pandas, who will try to steal the player's cake. You will learn how to bring characters to life by using the powerful animation system of Unity Mechanim, starting from Sprite-sheets.
Chapter 5, The Secret Ingredient Is a Dash of Physics, takes you by hand deep down into the secrets of physics and explains them in a clear and easy way. You will grasp the basics of physics, and learn how to use the 2D Physics engine of Unity.
Chapter 6, Through a Sea of Sprinkles – Navigation in Artificial Intelligence, is an introduction to the universe of artificial intelligence applied to video games. You will learn the basics principles and how to implement a navigation system in Unity for 2D (or even 3D) games, so that we can make our terrible sweet-toothed Pandas move!
Chapter 7, Trading Cupcakes and the Ultimate Battle for the Cake – Gameplay Programming, wraps everything we saw and learned in the previous chapters to conclude the tower defense game. In particular, we will deal with gameplay programming, which is the glue between the different parts of your game.
Chapter 8, What Is beyond the Cake?, explores different aspects of our game and game development in general. You will discover different tips, tricks, and suggestions on how to improve the game we built in this book, along with improving your own skills to become a better game developer. Then, the chapter will give you an understanding of parts of the game development pipeline, which are not strictly tied to the development of the game itself. Thus, you will dive into different topics ranging from playtesting, optimizations, team management and work, documentation, getting ready to publish your game, marketing, social media, game protections, to even localization.
What you need for this book
To go through the topics covered in this book, you will need two important things:
Any version of Unity, possibly 5.x since it was the range of versions that this book has been written for. As you already know, and Chapter 1, A Flat World in Unity, will repeat, you can get it from the Unity official website: http://www.unity3d.com.
Your enthusiasm and passion, in order to take an awesome journey in the world of game development together!
Who this book is for
Anyone including students, game enthusiasts, and academic professors who want to adopt this book as part of their course and classes.
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, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: All of them are placed inside a folder named Assets, which can be found inside the Project folder.
A block of code is set as follows:
public AwesomeUnityDeveloper GettingStarted(Yourself you) {
you.ReadThisBook();
return you;
}
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
public AwesomeUnityDeveloper GettingStarted(Yourself you) { you.ReadThisBook();
return you;
}
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: First of all, if you ever need to switch between 2D and 3D mode, you can do so by navigating to Edit | Project Settings | Editor.
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 feedback@packtpub.com, 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 code
You can download the example code files for this book 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.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Getting-Started-with-Unity-5.x-2D-Game-Development. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Downloading the color images of this book
We also provide you with 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/GettingStartedwithUnity5x2DGameDevelopment_ColorImages.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 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 copyright@packtpub.com 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 questions@packtpub.com, and we will do our best to address the problem.
Chapter 1. A Flat World in Unity
This is the beginning of our journey in to the world of 2D game development with Unity. This book is structured to guide you through the creation of an entire 2D game from scratch, in particular a tower defense game.
Despite the fact that we will focus on 2D game development, our final goal is to learn how to use Unity, and therefore this means that we will also have a glance at how Unity handles 3D. So, if later on you want to dedicate some time to 3D games, by the end of this book you will have the necessary background for doing so. In fact, the book is structured to contain as much detail as possible for each topic, and this includes historical overviews and references to further readings.
In every chapter, we will face different challenges that will improve our skills. Furthermore, this book doesn't stop by telling you just what needs to be done (like many others), but it also explains the different tools that we will encounter and how to use them. In this way, we will learn how to use and apply them in different contexts as well. Thus, you can use this book as a reference manual, in order to speed up your workflow. To help you out, I recommend that you use the index, to quickly locate each one of the specific topics we will face.
At the end of each chapter, there is a Homework section, which leaves you with some exercises that are related to the topics that we have dealt in that chapter. Of course, you are free to skip this section, but I recommend that you do the exercises if you feel that you need to improve your skills even more.
For now, this chapter is an introduction to the 2D world inside Unity, and what needs to be done in order to create our game. In particular, we will look at the following topics:
What are 2D games?
What does designing and developing 2D games mean?
Where to get Unity and its different versions
Downloading a graphical package from an external source
How to organize a project in Unity
Understanding Unity when it is set in 2D
What are Sprites?
The Sprite Renderer component
Import settings for Sprites
How to use the Sprite Editor with all its different modes
Preparing the assets for our game
Setting up scenes and proportions
One last thing. Sometimes I'll refer to the player and to characters in order to make examples or explain concepts. As such, sometimes I'll refer to them as if they were males, and at other times as if they were females (and sometimes both). The reason derives from my personal point of view so as to not to discriminate between the two genders.
And with this said, let's get started!
Learning game development
Game development and design are some of the most extensive works of art around. This is due to the large amount of expertise that is required to bring a game to life. You can get an idea of this by just looking at any credits in a game. They are extensive and contain a lot of names of people who have dedicated a lot of their time to the game in various roles.
Like most things in life, game development can be learned not only through practice, but iteration as well. And even when you master one of the many branches in game development, there is still something new to learn.
Regardless of your level of knowledge of Unity, I strongly suggest that you follow each step in this book, even if you think you know the topic. You just never know, there is always something new to learn!
Tower defense games
Tower defense games come in many different styles. For example, in the following screenshot of Defense Grid: The Awakening and Unstoppable Gorg, both are top-down isometric style games. However, they are set in different worlds, with different objectives. So, given this, what makes a tower defense game? For one, they are centered on the idea of defending something, whether it is buildings, resources, weapons, and so forth. This is the main mechanism that defines the genre and drives the gameplay. Secondly, most tower defense games require economic and resource management. For example, after each wave of enemies, you may obtain a certain amount of virtual currency that you must then allocate to either the purchase of new defenses (weapons, towers, and so on) or upgrades. Each has their benefits depending on a range of factors such as weak points in your defense as well as the anticipated amount and strength of enemies that will be in the next wave. The number and difficulty of enemies increases after each wave, therefore challenging the player to manage resources and build defenses strategically. The idea is to build up enough resources to upgrade your defenses and to outlast the incoming wave. Sometimes, the player must stop the enemies (or an opponent) from destroying their own base.
In other cases, the player must prevent the enemies from reaching the end, as each enemy that manages to get through cause damage to the player's health bar.
(Top) Defense Grid: The Awakening and (bottom) Unstoppable Gorg
There are many places on the Internet where you can find tower defense games. For example, Kongregate (http://www.kongregate.com/) and Newgrounds (http://www.newgrounds.com/) are examples of sites where a varied array of free tower defense games exist (such as Kingdom Rush or Bloons Tower Defense 5). However, many exist for iOS (App Store) and Android (Play Store), operating systems such as Linux, OSX, and PC (for example, Steam), and consoles (Playstation, Xbox), and so on.
Designing our game
Before you even think about turning your computer on, you need to design your game. It's not enough to have a rough idea in mind. You need to write down all your ideas before hand to start working. The first phase of game design is brainstorming. There are different techniques and methodologies of brainstorming that, unfortunately, we don't have the time, in this small section, to describe. However, the final outcome should be tons of paper with thousands of ideas written down. It's not meant to be a work of art, but the foundations upon which your game will be built.
Note
Some information about brainstorming can be found also in a practical book, Gamification with Unity, by Packt publishing. You can find it here: https://www.packtpub.com/game-development/gamification-unity-5x.
The next step is to refine your ideas, discard (or keep for other projects) the ones that you don't need, and organize them in a coherent form.
The final result should be something like the following.
Panda Invasion is a 2D tower defense game. In fact, hungry pandas are invading to steal all the sugar from the player. He or she has to push back the pandas by using cupcake towers. There are different kinds of cupcake towers that the player can decide to place in the map. In every level, there will be a path that pandas will follow. Furthermore, they are spawned at the beginning of this path. At the end, there is the ambitious sugar castle that the player has to defend. If the pandas steal too much, bringing the sugar-meter to zero, the player has failed his or her important mission. On the contrary, if he or she is able to push all of them back, the player will gain victory. However, cupcake towers are not free. In fact, the player has to buy them by using candy. Every time that a panda is pushed back, then the player will earn a certain amount of candy. Furthermore, the player can use candy to upgrade the cupcake towers and make them even stronger!
From this excerpt, you are now able to understand what we are going to do in this book. You also have a basic idea of how to write down your ideas. I strongly suggest that you always do this step, even when you are the only developer, and especially when you have a team.
Getting ready
Now that we have our idea, the next thing to do is to get Unity. It comes in different versions: Personal (which is free), Plus, Professional, and Enterprise. The last three contain more features than the Personal one. However, all the topics covered in this book can be done with the free version. In any case, you can get or buy Unity on the official website: www.unity3d.com.
This is the comparison screen between the different versions of Unity on the Unity Technologies website (if you scroll down, you will find which feature is included in which version):
Note
This is a very short summary of how model pricing for Unity has varied in recent years. In fact, to become a developer also means to be aware of the world around you, and having basic marketing knowledge could also help. At the beginning, the model price of Unity didn't allow developers to publish commercial games with the free version. In addition, the game engine didn't have all the features, such as the Profiler or the Movie Textures. Epic Games, the company that owns Unreal Engine, changed its model prices by making its game engine free in March 2015, also for commercial use (although it will take in return the 5% of the game's gross revenue). After a period of time, Unity Technologies also allowed developers to publish commercial games even with the free version, but it was still watermarked. From Unity 5.x, features that were only in the Pro version became available in the free version as well. During the beginning of 2016, Unity used to come in two different versions: Free (or Personal) and Professional. The latter contains more features than the Personal one, and here is the comparison screen of the two:
In June 2016, Unity changed its price model in the one described earlier.
Once we have installed Unity, we can begin creating new projects. If we click on the New project button in the top-right corner of the window, Unity will ask us to insert the details for our project. We can name it Panda Invasion and choose the destination path, which is