Mobile Game Development with Unity: Build Once, Deploy Anywhere 1st Edition Jonathon Manning download
Mobile Game Development with Unity: Build Once, Deploy Anywhere 1st Edition Jonathon Manning download
https://textbookfull.com/product/mobile-game-development-with-
unity-build-once-deploy-anywhere-1st-edition-jonathon-manning/
https://textbookfull.com/product/build-mobile-apps-with-
ionic-2-and-firebase-hybrid-mobile-app-development-1st-edition-
fu-cheng/
https://textbookfull.com/product/getting-started-with-
unity-2018-a-beginner-s-guide-to-2d-and-3d-game-development-with-
unity-3rd-edition-edward-lavieri/
https://textbookfull.com/product/android-continuous-integration-
build-deploy-test-automation-for-android-mobile-apps-pradeep-
macharla/
Unity 2017 Game Development Essentials Build fully
functional 2D and 3D games with realistic environments
sounds physics special effects and more Tommaso
Lintrami
https://textbookfull.com/product/unity-2017-game-development-
essentials-build-fully-functional-2d-and-3d-games-with-realistic-
environments-sounds-physics-special-effects-and-more-tommaso-
lintrami/
https://textbookfull.com/product/learn-unity-for-android-game-
development-a-guide-to-game-design-development-and-marketing-1st-
edition-adam-sinicki/
https://textbookfull.com/product/build-mobile-apps-with-
ionic-4-and-firebase-hybrid-mobile-app-development-lingua-
inglese-2nd-edition-fu-cheng/
https://textbookfull.com/product/beginning-ios-ar-game-
development-developing-augmented-reality-apps-with-unity-
and-c-1st-edition-allan-fowler/
https://textbookfull.com/product/unity-game-development-cookbook-
essentials-for-every-game-1-converted-edition-paris-buttfield-
addison/
Mobile Game
Development
with Unity
BUILD ONCE, DEPLOY ANYWHERE
“If you want to build any kind of game for mobile platforms, you’ve got
to take a look at Unity. This book is an excellent, thorough, and
seriously fun guide to putting together gameplay in one of the best
game engines out there for indie developers.”
—Adam Saltsman, Creator of Canabalt and
Overland at Finji
“The best way to learn how to use a game engine is by getting your
hands dirty and building your own projects. In this book, Paris and Jon
guide you through the creation of two radically different games, giving
you invaluable hands-on experience with a wide range of Unity’s
features.”
—Alec Holowka, Lead Developer of Night in the
Woods and Aquaria at Infinite Ammo
“This book changed my life. I now feel inner peace, and I’m pretty sure
I can see through time.”
—Liam Esler, Game Developers’ Association of
Australia
Mobile Game Development
with Unity
Build Once, Deploy Anywhere
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Mobile Game
Development with Unity, the cover image, and related trade dress are trademarks of
O’Reilly Media, Inc.
While the publisher and the authors have used good faith efforts to ensure that the
information and instructions contained in this work are accurate, the publisher and
the authors disclaim all responsibility for errors or omissions, including without
limitation responsibility for damages resulting from the use of or reliance on this
work. Use of the information and instructions contained in this work is at your own
risk. If any code samples or other technology this work contains or describes is sub‐
ject to open source licenses or the intellectual property rights of others, it is your
responsibility to ensure that your use thereof complies with such licenses and/or
rights.
978-1-491-94474-5
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
2. A Tour of Unity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
The Editor 7
The Scene View 11
The Hierarchy 14
The Project View 15
The Inspector 17
The Game View 19
Wrapping Up 19
3. Scripting in Unity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
A Crash Course in C# 22
Mono and Unity 23
Game Objects, Components, and Scripts 25
Important Methods 28
Coroutines 31
Creating and Destroying Objects 33
Attributes 36
Time in Scripts 39
v
Logging to the Console 40
Wrapping Up 40
vi | Table of Contents
Part III. Building a 3D Game: Space Shooter
9. Building a Space Shooter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Designing the Game 204
Architecture 209
Creating the Scene 210
Wrapping Up 226
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
ix
Unity supports a few different programming languages. We’ll be
using C# in this book. We will assume that you know how to pro‐
gram in a relatively modern language, but it doesn’t have to be
recent programming experience as long as you’re somewhat com‐
fortable with the basics.
The Unity editor runs on both macOS and Windows. We use
macOS, so the screenshots shown throughout the book are taken
from there, but everything we cover is identical on Windows, with
one small exception: building iOS games with Unity. We’ll explain
when we get to it, but you can’t do it on Windows. Android works
fine on Windows though, and macOS can build for both iOS and
Android.
The book takes the approach that you need to understand the basics
of game design, as well as Unity itself, before you build some games,
so we teach you that in Part I. Once that’s done, parts II and III
explore the construction of a 2D game and a 3D game, respectively,
and then in Part IV we follow up with all the other Unity features
that you should know about.
We will assume that you’re fairly confident and comfortable navigat‐
ing your operating system, and using your mobile devices (whether
they be iOS or Android).
We won’t be covering the creation of art or sound assets for your
games, although we do supply assets for the two games you build
through this book.
x | Preface
Constant width bold
Shows commands or other text that should be typed literally by
the user.
Constant width italic
Shows text that should be replaced with user-supplied values or
by values determined by context.
Preface | xi
“Mobile Game Development with Unity by Jonathon Manning and
Paris Buttfield-Addison (O’Reilly). Copyright 2017 Jon Manning
and Paris Buttfield-Addison, 978-1-491-94474-5.”
If you feel your use of code examples falls outside fair use or the per‐
mission given above, feel free to contact us at permis‐
sions@oreilly.com.
O’Reilly Safari
Safari (formerly Safari Books Online) is a
membership-based training and reference
platform for enterprise, government, educa‐
tors, and individuals.
How to Contact Us
Please address comments and questions concerning this book to the
publisher:
We have a web page for this book, where we list errata, examples,
and any additional information. You can access this page at http://
bit.ly/Mobile-Game-Dev-Unity.
xii | Preface
To comment or ask technical questions about this book, send email
to bookquestions@oreilly.com.
For more information about our books, courses, conferences, and
news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Acknowledgments
Jon and Paris wish to thank their fabulous editors, especially Brian
MacDonald (@bmac_editor) and Rachel Roumeliotis (@rroumelio‐
tis) for their work in bringing this book to fruition. Thanks for all
the enthusiasm! Thanks also to the fabulous staff at O’Reilly Media,
for making writing books such a pleasure.
Thanks also to our families for encouraging our game development,
as well as all of MacLab and OSCON (you know who you are) for
encouragement and enthusiasm. Thanks particularly to our fabulous
tech reviewer, Dr. Tim Nugent (@the_mcjones).
Preface | xiii
PART I
The Basics of Unity
To kick off our exploration of the Unity game engine, we’ll start with
the basics: what Unity is, what it’s useful for, and how to get it. At the
same time, we’ll set up some useful constraints for the subject mate‐
rial we’re looking at in this book; after all, you’re holding a book that
claims to be about mobile development, not all development. Such a
book would be much heavier, or would make your reading software
crash. We aim to spare you this misfortune.
Hello, Book
Before we dive into Unity itself, let’s take a closer look at what we’re
talking about here: the field of mobile games.
Mobile Games
So, what is a mobile game, and how is it different from any other
sort of game? More practically, how do these differences factor into
your decisions when you’re both designing and later implementing a
game?
Fifteen years ago, a mobile game was likely to be one of two things:
3
• A much more complex affair, available only on specialized
mobile gaming consoles, and created by companies with access
to expensive dev kits for said mobile gaming consoles
This split was the result of both hardware complexity and distribu‐
tion availability. If you wanted to make a game that was in any way
complex (and by complex we mean featured the incredible ability to
have more than one thing moving on the screen at a time), you
needed the more advanced computing power available only on
expensive portable consoles, like Nintendo’s handheld devices.
Because the console owners also owned the distribution channels
for the games, and wanted to have a high degree of control, getting
permission to make games for more capable hardware became a
challenge.
However, as more powerful hardware became cheaper over time,
more options opened up for developers. In 2008, Apple made its
iPhone available to software developers, and in the same year Goo‐
gle’s Android platform became available. Over the years, iOS and
Android have become extremely capable platforms, and mobile
games are the most popular video games in the world.
These days, a mobile game is typically one of three things:
You can use Unity to do all three of these; in this book, we’ll be con‐
centrating on the first approach. After exploring Unity and how it’s
used, we’ll step through the creation of two games that fit those fac‐
ets.
Hello, Unity
Now that we’ve elaborated a bit on what we’re trying to make, let’s
talk about what we’re going to make it with: the Unity game engine.
Hello, Unity | 5
you don’t need it anyway), but that there are certain things that
are out of your hands.
Getting Unity
Unity is available for Windows, macOS, and Linux. Unity comes in
three main flavors: Personal, Plus, and Pro.
The features of the Unity software are largely the same across each
edition. The main difference between the free and paid editions is
that the Personal edition imposes a splash screen on your game,
which shows the Unity logo. The free edition is only available to
individuals or organizations that have a revenue of $100,000 a year
or less, while the limit for Plus is $200,000. Plus and Pro also include
slightly better services, such as priority build queues in Unity’s
Cloud Build service (discussed in more detail in “Unity Cloud
Build” on page 423).
To download Unity, head to https://store.unity.com. Once you’ve
installed it, you’re ready to get going, and we’ll see you in the next
chapter.
Once you’ve got Unity installed, it’s helpful to spend a bit of time
learning your way around it. Unity’s user interface is reasonably
straightforward, but there are enough individual pieces that it’s
worth taking some time to review it.
The Editor
When you fire up Unity for the first time, you’ll be asked to provide
your license key, and you’ll be asked to sign in to your account. If
you don’t have one, or if you don’t want to sign in, you can skip the
login.
Once you’re past that point, you’ll be taken to Unity’s start screen,
where you can choose to either create a new project, or open an
existing one (Figure 2-1).
7
Figure 2-1. Unity’s splash screen, when signed in
If you click on the New button at the top-right, Unity will ask you
for some information for it to use while setting up the project
(Figure 2-2), including the name of the project, where to save it, and
whether you’d like Unity to create a 2D or 3D project.
When you click the “Create project” button, Unity will generate the
project on disk for you and open it in the editor (Figure 2-3).
The Editor | 9
Project Structure
Unity projects are not single files; instead,
they’re folders, which contain three important
subfolders: Assets, ProjectSettings, and Library.
The Assets folder contains all of the files that
your game uses: your levels, textures, sound
effects, and scripts. The Library folder contains
data that’s internal to Unity, and the ProjectSet‐
tings folder contains files that contain your proj‐
ect’s settings.
You don’t generally need to touch any file inside
the Library and ProjectSettings folders.
Additionally, if you’re using a source control sys‐
tem like Git or Perforce, you don’t need to check
the Library folder into your repository, but you
do need to check in the Assets and ProjectSet‐
tings folders in order to make sure that your col‐
laborators have the same assets and settings as
you.
If all of that sounded unfamiliar, you can safely
ignore it, but we do strongly suggest following
proper source control standards for your code—
it can be extremely useful!
Unity is designed around the use of several panes. Each pane has a
tab at its top left, which can be dragged around to change the layout
of the application. You can also drag a tab out and make it a separate
window. Not all of Unity’s panes are visible by default, and as you
build your game, you’ll end up opening more of them via the Win‐
dow menu.
While in Play Mode, you can temporarily pause the game by press‐
ing the Pause icon in the middle of the Play Mode controls. Press it
again to resume playback. You can also ask Unity to advance a single
frame and then pause again by clicking the Step button at the far
right.
Let’s now take a closer look at the tabs that appear by default. In this
chapter, we’ll refer to the location of the panes as they appear in the
default layout. (If you can’t see one of the panes, make sure you’re
using the default layout.)
Figure 2-5. The scene view’s mode selector, shown here in Translate
mode
You can switch the mode that the scene view is in using the mode
selector; alternatively, you can press the Q, W, E, R, and T keys to
quickly switch between them.
Getting Around
There are a few ways to get around in the scene view:
• Click the Hand icon at the top-left of the window to enter Grab
mode, and left-click and drag to pan the view.
• Hold down the Option key (Alt on a PC) and left-click and drag
to rotate the view.
• Select an object in the scene by left-clicking on it in the scene, or
clicking on its entry in the Hierarchy (which we’ll talk about in
“The Hierarchy” on page 14), move the mouse over the scene
view, and press F to focus the view on the selected object.
• Hold down the right mouse button, and move the mouse to
look around; while you’re holding the right mouse button, you
can use the W, A, S, and D keys to fly forward, left, back, and
right. You can also use the Q and E keys to fly up and down.
Hold the Shift key to fly faster.
Handle Controls
To the right of the mode selector, you’ll find the handle controls
(Figure 2-6). The handle controls determine where the handles—the
movement, rotation, and scaling controls that appear when you
select an object—should be positioned and oriented.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com