Table Of ContentS
BOOKS FOR PROFESSIONALS BY PROFESSIONALS® u
m
p
te
r
M
Make a 2D Arcade Game in a Weekend a
k
e
a
Create and complete your fi rst 2D arcade game in Unity. In Make a 2D Arcade Game
2
in a Weekend with Unity, you will learn to create an arcade classic brick breaker D
game from beginning to end. You will plan the game fl ow, add the graphics and
A
create the game logic using C#, then build the UX to complete your game. By the r
c
time you have fi nished, you will have enough knowledge to tweak the game to a
d
create more levels or your own variant game rules, and you will have the confi dence
e
to go on and create your own 2D arcade games. You will also learn how to publish G
the game into mobile app stores. a
m
e
Unity is a powerful cross platform soft ware tool that allows you to create 2D and 3D Make a
i
apps and games. Learning how to create a classic arcade game is a great way to learn n
the foundations of game design. While you do need to have a basic understanding a
W
of Unity to complete this project, advanced game building or advanced Unity
experience is not required. e 2D Arcade Game
e
k
• Learn how to design a classic arcade puzzle game with a unique twist e
n
• Navigate the Unity game development platform
d
• Gain knowledge and experience scripting in C#
• Add power-ups and scoring to a game in a Weekend
• Learn tips for shortening the development timeline
• Deploy your game using Unity Cloud Build
With Unity
—
Create your fi rst 2D arcade game in a
weekend
—
IISSBBNN 99787-81--14-84428-4124-951-4495-4Shelve in Jodessiah Sumpter
Graphics/Game Programming
User level
Beginning
9 781484 214954 SOURCE CODE ONLINE
www.apress.com
www.it-ebooks.info
Make a 2D Arcade
Game in a Weekend
With Unity
Jodessiah Sumpter
www.it-ebooks.info
Make a 2D Arcade Game in a Weekend: With Unity
Copyright © 2015 by Jodessiah Sumpter
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part
of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission
or information storage and retrieval, electronic adaptation, computer software, or by similar or
dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are
brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for
the purpose of being entered and executed on a computer system, for exclusive use by the purchaser
of the work. Duplication of this publication or parts thereof is permitted only under the provisions
of the Copyright Law of the Publisher’s location, in its current version, and permission for use must
always be obtained from Springer. Permissions for use may be obtained through RightsLink at the
Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
ISBN-13 (pbk): 978-1-4842-1495-4
ISBN-13 (electronic): 978-1-4842-1494-7
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and
images only in an editorial fashion and to the benefit of the trademark owner, with no intention of
infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they
are not identified as such, is not to be taken as an expression of opinion as to whether or not they are
subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal responsibility
for any errors or omissions that may be made. The publisher makes no warranty, express or implied,
with respect to the material contained herein.
Managing Director: Welmoed Spahr
Lead Editor: Ben Renow-Clarke
Technical Reviewer: Marc Schärer
Edit orial Board: Steve Anglin, Pramila Balan, Louise Corrigan, James T. DeWolf,
Jonathan Gennick, Robert Hutchinson, Celestin Suresh John, Michelle Lowman,
James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick,
Ben Renow-Clarke, Gwenan Spearing
Coordinating Editor: Mark Powers
Copy Editor: Karen Jameson
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC
and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc).
SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional
use. eBook versions and licenses are also available for most titles. For more information, reference
our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available
to readers at www.apress.com/9781484214954. For detailed information about how to locate your
book’s source code, go to www.apress.com/source-code/. Readers can also access source code at
SpringerLink in the Supplementary Material section for each chapter.
www.it-ebooks.info
Contents at a Glance
About the Author ������������������������������������������������������������������������������ix
About the Technical Reviewer ����������������������������������������������������������xi
Acknowledgments ��������������������������������������������������������������������������xiii
Introduction �������������������������������������������������������������������������������������xv
■ Chapter 1: Getting Started ��������������������������������������������������������������1
■ Chapter 2: Define Game Layout and Environment ������������������������23
■ Chapter 3: Create Positioning and Movement ������������������������������41
■ Chapter 4: Scripting a Game Manager �����������������������������������������61
■ Chapter 5: Adding Sound and Music ��������������������������������������������83
■ Chapter 6: Game Power-Ups ���������������������������������������������������������97
■ Chapter 7: Level Manager and Menu ������������������������������������������115
■ Chapter 8: Publishing to the App Store ��������������������������������������139
Index ����������������������������������������������������������������������������������������������157
iii
www.it-ebooks.info
Contents
About the Author ������������������������������������������������������������������������������ix
About the Technical Reviewer ����������������������������������������������������������xi
Acknowledgments ��������������������������������������������������������������������������xiii
Introduction �������������������������������������������������������������������������������������xv
■ Chapter 1: Getting Started ��������������������������������������������������������������1
Prerequisites �������������������������������������������������������������������������������������������1
Install Unity ���������������������������������������������������������������������������������������������2
Choosing a License ���������������������������������������������������������������������������������������������������4
Welcome to Unity ������������������������������������������������������������������������������������������������������6
Configuring Unity ������������������������������������������������������������������������������������������������������8
Skinning the Interface ���������������������������������������������������������������������������10
Learning the Views �������������������������������������������������������������������������������������������������11
Understanding Layouts �������������������������������������������������������������������������������������������16
Development Tips and Tricks �����������������������������������������������������������������18
Changing Runtime Background Color ���������������������������������������������������������������������18
Useful Hotkeys ��������������������������������������������������������������������������������������������������������20
Summary �����������������������������������������������������������������������������������������������21
■ Chapter 2: Define Game Layout and Environment ������������������������23
2D Game Design Setup��������������������������������������������������������������������������23
Laying Out the Game �����������������������������������������������������������������������������25
Importing Assets �����������������������������������������������������������������������������������26
v
www.it-ebooks.info
■ Contents
Define Screen Resolution ����������������������������������������������������������������������28
Adding Background �������������������������������������������������������������������������������29
Adjusting Our Camera ���������������������������������������������������������������������������32
Adding Bricks ����������������������������������������������������������������������������������������32
Creating PreFabs ����������������������������������������������������������������������������������������������������33
Creating Row of Bricks �������������������������������������������������������������������������������������������35
Add the Paddle �������������������������������������������������������������������������������������������������������36
Add the Ball �������������������������������������������������������������������������������������������38
Summary �����������������������������������������������������������������������������������������������39
■ Chapter 3: Create Positioning and Movement ������������������������������41
Making Our Ball Move ���������������������������������������������������������������������������41
Handling Ball Collisions �������������������������������������������������������������������������44
Making Our Ball Bounce ������������������������������������������������������������������������46
Beginning Scripting �������������������������������������������������������������������������������47
The Rules of C Sharp (C#) ���������������������������������������������������������������������������������������48
Selecting an Editor �������������������������������������������������������������������������������������������������48
Our First Script �������������������������������������������������������������������������������������������������������48
Moving the Paddle with the Mouse ������������������������������������������������������������������������52
Launching the Ball with the Mouse ������������������������������������������������������������������������54
Destroy the Blocks on Hit ���������������������������������������������������������������������������������������57
Summary �����������������������������������������������������������������������������������������������60
■ Chapter 4: Scripting a Game Manager �����������������������������������������61
Keeping Our Ball in the Game Space �����������������������������������������������������61
Creating Invisible Collider Walls ������������������������������������������������������������62
Changing the Impact of Gravity �������������������������������������������������������������64
Relabeling the Game Objects in Our Scene �������������������������������������������66
Scripting Our Game Manager ����������������������������������������������������������������67
vi
www.it-ebooks.info
■ Contents
Scripting Our Lose Collider ��������������������������������������������������������������������71
Using UI Text to Display Information ������������������������������������������������������75
Summary �����������������������������������������������������������������������������������������������82
■ Chapter 5: Adding Sound and Music ��������������������������������������������83
Adding Background Music ��������������������������������������������������������������������83
Adding Start and End Sounds ����������������������������������������������������������������85
Bricks with Action and Impact Sound ���������������������������������������������������86
Modify Brick Script for Sound ���������������������������������������������������������������87
Game Area Sounds for the Walls and Paddle ����������������������������������������92
Summary �����������������������������������������������������������������������������������������������95
■ Chapter 6: Game Power-Ups ���������������������������������������������������������97
Building Power-Up Scripts ��������������������������������������������������������������������97
Building Base Power-Up Prefab Scripts ������������������������������������������������99
Extra Balls Script ���������������������������������������������������������������������������������100
Change Paddle Size Script ������������������������������������������������������������������105
Creating Prefab Game Objects for Ball and Paddle Changes ��������������106
Sprites for Prefabs ������������������������������������������������������������������������������������������������107
Extra Ball Prefab ���������������������������������������������������������������������������������������������������107
Shrink and Grow Prefabs ��������������������������������������������������������������������������������������108
Putting it All Together in the Scene �����������������������������������������������������111
Modify the Lose Script ������������������������������������������������������������������������113
Summary ���������������������������������������������������������������������������������������������114
■ Chapter 7: Level Manager and Menu ������������������������������������������115
Creating Intro Scene ����������������������������������������������������������������������������115
Add a Main Title and Buttons ��������������������������������������������������������������������������������115
Script for Loading a Level �������������������������������������������������������������������118
Modifying the Game Lose Scenario �����������������������������������������������������121
vii
www.it-ebooks.info
■ Contents
Add Restart and Main Menu Buttons ��������������������������������������������������������������������121
Add a Panel for the Buttons ����������������������������������������������������������������������������������121
Updating the GameManager and Lose Scripts �����������������������������������������������������122
Rename and Duplicate Main Scene ����������������������������������������������������128
Modifying Level 1 to Include Level Manager ���������������������������������������128
Adding Scenes to the Build Settings ���������������������������������������������������130
Add Additional Buttons ������������������������������������������������������������������������131
Background Music ������������������������������������������������������������������������������134
Trailing Ball Effect �������������������������������������������������������������������������������134
Summary ���������������������������������������������������������������������������������������������137
■ Chapter 8: Publishing to the App Store ��������������������������������������139
Investigating Deployment Options �������������������������������������������������������139
Defining Build Settings ������������������������������������������������������������������������140
Adding a Quit Button ���������������������������������������������������������������������������������������������144
Button Creation and LevelLoader Script Modification ������������������������������������������145
Deploying to WebGL ����������������������������������������������������������������������������146
Unity Cloud Build ���������������������������������������������������������������������������������148
Placing Our Game in GitHub ���������������������������������������������������������������������������������149
Adding your GitHub to the Cloud ���������������������������������������������������������������������������152
Summary ���������������������������������������������������������������������������������������������155
Index ����������������������������������������������������������������������������������������������157
viii
www.it-ebooks.info
About the Author
Jodessiah Sumpter currently is the Chief Technology
Officer for numerous start-up companies including
Perfomatix Innovations and Food Cowboy. He has
over 10 years of software development experience at
Fortune 500 companies and has 20+ years experience
developing websites and Internet marketing materials
for individuals, non-profits, start-ups and small
businesses.
Joe has also developed and deployed numerous
mobile and TV applications for Android, iOS,
Blackberry, Windows, and Samsung Smart TV. His
company Blue Crystal Web Design has won numerous
awards with the most recent being the AT&T U-Verse Hackathon and the Extreme Reality
Android Challenge.
Joe is a serial entrepreneur who specializes in taking the software ideas of his national
clients from concept to reality. He received an MBA from the University of Buffalo and a
Post Masters degree in Marketing from the University of Dayton. You can reach find him
on Twitter @bcwdesign or view his blog at http://www.bluecrystalwebdesign.com.
ix
www.it-ebooks.info
About the Technical
Reviewer
Marc Schärer is an interactive media software
engineer and contributor to the Unity forums as a
fulltime professional unity user since 2007–2008.
As a Swiss local, he attempts to support the local
development communities in Switzerland to help
them unleash their potential, applying his experience
delivering interactive 3d learning, training and
entertainment experiences to mobile, desktop and
web platforms for customers around the world.
He has a strong background in the 3D graphics,
network technology, software engineering and
interactive media fields, which first interested him
as a teenager. He studied Computer Science and
Computational Science and Engineering at the Swiss
Federal Institute of Technology in Zürich.
He is currently the Chief VR Officer at vantage.tv, a
company he co-founded in 2014, which seeks to revolutionize how we see and experience
events of any type and scale in the future by removing the barrier of distance.
xi
www.it-ebooks.info