Table Of Contentwww.it-ebooks.info
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
www.it-ebooks.info
Contents at a Glance
About the Author ���������������������������������������������������������������������������������������������������������������xxi
About the Technical Reviewer �����������������������������������������������������������������������������������������xxiii
Acknowledgments ������������������������������������������������������������������������������������������������������������xxv
Introduction ��������������������������������������������������������������������������������������������������������������������xxvii
■ Chapter 1: Getting Started �������������������������������������������������������������������������������������������������1
■ Chapter 2: A Unity Tour ����������������������������������������������������������������������������������������������������19
■ Chapter 3: Making a Scene ����������������������������������������������������������������������������������������������61
■ Chapter 4: Making It Move: Scripting the Cube �������������������������������������������������������������113
■ Chapter 5: Let’s Dance! Animation and Sound ��������������������������������������������������������������149
■ Chapter 6: Let’s Roll! Physics and Controls �������������������������������������������������������������������175
■ Chapter 7: Let’s Bowl! Advanced Physics ���������������������������������������������������������������������213
■ Chapter 8: Let’s Play! Scripting the Game ���������������������������������������������������������������������255
■ Chapter 9: The Game GUI �����������������������������������������������������������������������������������������������283
■ Chapter 10: Using Unity iOS �������������������������������������������������������������������������������������������309
■ Chapter 11: Building for Real: Device Testing and App Submission ������������������������������337
■ Chapter 12: Presentation: Screens and Icons ���������������������������������������������������������������373
■ Chapter 13: Handling Device Input ��������������������������������������������������������������������������������401
v
www.it-ebooks.info
vi Contents at a Glance
■ Chapter 14: Game Center: Leaderboards and Achievements ����������������������������������������417
■ Chapter 15: iAd: Banner Ads and Interstitial Ads ����������������������������������������������������������439
■ Chapter 16: Optimization �����������������������������������������������������������������������������������������������453
■ Chapter 17: Where to Go from Here? �����������������������������������������������������������������������������499
Index ���������������������������������������������������������������������������������������������������������������������������������515
www.it-ebooks.info
Introduction
How I Met Unity
Technically, I first started programming on a TRS-80 in my junior high school library, but really I just
typed in the same BASIC code listing from a magazine every day until the librarian mentioned I could
save the program on a cassette. I’m embarrassed to recall my first reaction when I heard the library
had a computer: “What’s it good for?”
A year later, I saw the light when I got my hands on an Apple II. After cracking open the user manual
and learning how to draw graphics in BASIC, I was hooked. Soon I was writing Reversi games
(one in BASIC, one in 6502 assembly) and even a 3D wireframe display program.
In the intervening years I wandered the Windows wasteland and worked in small and large groups
developing computer graphics and games. But fast forward to six years ago, when I happily got
back into the Apple fold (now with Unix!) and attended my first Apple World Wide Developer
Conference. Joachim Ante, one of the cofounders and the CTO of Unity Technologies, gave me an
impromptu demo of Unity 1.5, and it was exactly what I’d been looking for—an inexpensive 3D game
engine that ran on a Mac and was able to target multiple platforms, including Windows, Mac, and
web browsers.
So I bought a Unity 1.5 Indie license as soon as I returned home (this was when Unity Indie wasn’t
free), later upgraded to Unity Pro, and was pleasantly surprised a couple of years later that Unity
would support iOS. I love it when a plan comes together!
In the meantime, my former employers at Hyper Entertainment granted me a license to port
HyperBowl, a 3D arcade bowling game I worked on over ten years ago, to Unity and various
platforms supported by Unity, so now I had a meaty project to work with, besides the smaller apps
I’d been experimenting with.
It took me six months to get the first version of the HyperBowl remake running as a Unity webplayer,
with standalone Mac and PC executables, and on the iPhone (and by the way, also Android, Linux,
and Flash). And really, I only spent three months using Unity if you subtract the time I spent figuring
out how to extract the art and audio assets from the original game.
xxvii
www.it-ebooks.info
xxviii Introduction
With each new version of Unity, HyperBowl and my other games got faster and better-looking,
utilized more iOS features and ran on more iOS devices (and, eventually, Android devices). I added
capabilities using third-party plugins, a new pause menu, and even an entire new HyperBowl lane
(level) with packages from the Unity Asset Store, which is conveniently integrated within the Unity
Editor.
This has all taken place with a development team of one (not counting all the work put into the
original licensed assets), and I didn’t have to learn a single line of Objective-C or create my own art.
In a sense, I feel like I’ve returned to my programming roots, working on my own projects for fun,
and as a bonus, profit! Hopefully, I can distill my experience with Unity over the past six years (both
mistakes and successes) into this book.
comfy confines of Unity (except when we have to dabble in Xcode when making iOS builds). We
will, however, do plenty of scripting with Unity’s version of JavaScript. Our focus will be on using the
built-in Unity script functions, but I’ll point you to Unity plugins and packages that provide further
capability.
You can find the source code on Apress.com at http://www.apress.com/9781430248750 or on
http://learnunity4.com/.
Explore Further
No one knows everything. That’s why a key to successful development is knowing how to find the
tools, assets, information, and help you need. So at the end of each chapter, I’ll suggest some
reading and resources for you to explore further.
I’ll start here, with a recommendation of other worthwhile Unity books. Even on the same topic, it’s
useful to read different books for their different takes on the subject. For example, Will Goldstone
wrote one of the first Unity books, Unity Game Development Essentials, Sue Blackman’s Beginning
Unity 3D Development is a hefty tome from an artist’s viewpoint that presents an adventure game,
and Jeff Murray covers Unity iOS in Game Development for iOS with Unity 3D, using a kart racing
game as an example.
Since I waxed nostalgic on Apple computers, I should follow up with some Apple historical reading.
Revolution in the Valley is a fun collection of Mac development anecdotes by Andy Hertzfeld. iWoz
by Steve Wozniak is an interesting peek at early Apple history and at the Woz himself, and
www.it-ebooks.info
Introduction xxix
Apple Design: The Work of the Apple Industrial Design Group by Paul Kunkel portrays the lineup of
classic Macs (and by classic, I mean everything before 1997).
Although this book makes heavy use of example game projects, there won’t be much discussion
on game design. But there’s certainly a lot of interesting reading on the subject. My favorite game
design book is Richard Rouse’s Game Design: Theory and Practice, mostly a collection of interviews
with famous game designers. And there’s a bounty of game design articles and blogs on the web
site Gamasutra (http://gamasutra.com/).
www.it-ebooks.info
1
Chapter
Getting Started
Unity is a cross-platform 3D game development system developed by a company called Unity
Technologies (originally named Over the Edge). What does it mean to call Unity cross-platform,
exactly? Well, it’s cross-platform in the sense that the Unity Editor, the game creation and editing
tool that is the centerpiece of Unity, runs on OS X and Windows. More impressively, Unity is
cross-platform in the sense that from the Unity Editor we can build games for OS X, Windows,
web browsers (using either Flash, Google Native client, or Unity’s browser plug-in), iOS, Android,
and game consoles. And the list keeps growing (shortly before this book’s publication, Unity
Technologies announced support for the BlackBerry 10)!
As for 3D, Unity is a 3D game development system in the sense that Unity’s built-in graphics, sound,
and physics engines all operate in 3D space, which is perfect for creating 3D games, but many
successful 2D games have also been developed in Unity.
This book describes the latest version of Unity as of this writing, which is Unity 4.1.2, but Unity is a
fast-moving target, with new features and user interface changes appearing even in minor releases
(which are appearing more frequently, since Unity Technologies has introduced a faster incremental
update schedule starting with Unity 4). This caveat applies to everything else, of course, including
products, licenses, and prices from Unity, Apple, and third-party vendors.
Prerequisites
Before the fun part, learning how to use the Unity Editor and build games, you need to download
Unity, install it, and activate a license. Although you’ll spend the first several chapters working with
step-by-step examples in the Unity Editor and not get into iOS development until later (by the way,
iOS originally stood for iPhone Operating System, but now includes the iPod Touch and iPad), it’s
not a bad idea to get started on the iOS development prerequisites, too.
1
www.it-ebooks.info
2 CHAPTER 1: Getting Started
Prepare Your Mac
For iOS development, you’ll need a Mac running the Lion or Mountain Lion version of OS X (that is,
version 10.7 and up). Unity 4 can still run on older versions of OS X, like Snow Leopard, but Lion and
Mountain Lion are currently required to run the latest version of Xcode, the software tool required by
Apple for iOS development. Typically, the latest or fairly recent version of Xcode is required to target
the latest version of iOS.
Register as an iOS Developer
It’s worth visiting the Apple developer site to register as an iOS developer as soon as possible, since
http://developer.apple.com/. When you start building Unity iOS apps, I’ll go over the Xcode
Download Unity
To obtain Unity, visit the Unity web site at http://unity3d.com/ and go to the Download page. There
you will find a download link for the latest version of Unity (at the moment, Unity 4.1.2) and also a
link to the release notes (which are included with the installation). There is also a link to a list of older
versions in case you need to roll back to a previous version of Unity for some reason.
Tip While you’re on the Unity web site, take a look around. Check out the demos, the FAQ, the feature
comparisons among the various licenses, and the support links to the documentation, user forum, and other
community support sites. You’ll certainly have to come back later, so you might as well figure out where
everything is now!
There is only one Unity application, with additional features and platform support activated by
licenses. For example, the product name Unity iOS Pro specifies Unity with an added Unity Pro
license and also a Unity iOS Pro license. I’ll elaborate on the various licenses a little bit later in this
chapter when I introduce the License Management window in Unity.
Unity version numbers are in the form major.minor.patch. So Unity 4.1.2 is Unity 4.0 with an
incremental upgrade to Unity 4.1, with a couple of bug fix updates. Major upgrades, such as Unity 3
to Unity 4, require a license upgrade purchase and may require changes to the project.
www.it-ebooks.info
CHAPTER 1: Getting Started 3
Tip In general, once a Unity project has been upgraded, it may become incompatible with older versions of
Unity. So it’s a good idea to make a copy of your project before upgrading it, just in case you need to revert
back to the previous version of Unity.
To start the Unity installation process, click the download link (as of this writing, it’s a button labeled
Download Unity 4.1.2). The file is around 1GB in size, so the download will take a while, but we’re on
our way!
Install Unity
The Unity download file is a disk image (DMG file), which at this time is a file named unity-4.1.2.dmg.
Once the file is downloaded, double-click it to see the disk image contents (Figure 1-1).
Figure 1-1. The Unity installer files
The installer disk image only contains two files: the release notes and the standard OSX installer
package for Unity in the form of a file named Unity.pkg.
www.it-ebooks.info
Description:Unity is an incredibly powerful and popular game creation tool, and Unity 4 brings even more great features, including Mechanim animation. Learn Unity 4 for iOS Game Development will show you how to use Unity with Xcode to create fun, imaginative 3D games for iPhone, iPad, and iPod touch. You'll lea