Mastering Unity 2D Game Development
Table of Contents
Mastering Unity 2D Game Development
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
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. Overview
Getting assets
Collection of sites
Welcome to 2D
The new 2D mode
Sprites
Sprite Renderer
Sprite Editor
Spritesheets
Texture atlases
Sprite meshes
The Box2D physics system
The new animation Dope Sheet
Other Unity 4.3 improvements
Improved Mecanim performance
The Windows Phone API improvements and Windows 8.1 support
Dynamic Nav Mesh (Pro version only)
Editor updates
MonoDevelop 4.01
Movie textures
Summary
2. Building a Character
Getting your project started – the right way
Structure
Object naming
Understanding components
Accessing components using a shortcut
A better way to use components – referencing
An even better way
Creating the project
Classes
The object-orientated design
The game structure
The common game object
The player object
More later
Planning behaviors
Behaviors for the common game object
Behaviors for the player's character
Introducing the Unity sprite system
Importing sprites
Texture type – sprite
Sprite Mode – single/multiple
Packing tag
Pixels to units
The Sprite Editor button
Sprite Editor
Sprite splitter (slicer)
Automatic
Grid (manual)
View controls
Sprite region manipulation
Putting it together
Importing our main character
The hero
Controlling the hero
Going further
Summary
3. Getting Animated
Sprite animation
Animation components
Animation controllers
Animation clips
The Animator component
Setting up animation controllers
Setting up animation clips
Manual animation clip creation
Automatic animation clip creation
Animator Dope Sheet
The time/recording controls
Selecting the animation clip
The sample rate (frames per second)
Animation properties
Timeline
Dope/curve view
Putting it together
Setting up the animation controller
Adding your first animation clip (idle)
Adding another animation clip (run)
Connecting animation states
Accessing controllers from a script
Extra credit
Getting curvy
Going further
Summary
4. The Game World
Backgrounds and layers
To slice or not to slice
The town background
The town buildings
The extra scenery
Building the scene
Adding the town background
Sprite sorting layers
Sprite sorting order
Updating the scene Sorting Layers
Working with the camera
Comparing Update, FixedUpdate, and LateUpdate
The perils of resolution
A better FollowCamera script
Transitioning and bounds
Towns with borders
Journeying onwards
Planning for the larger picture
Backgrounds and active elements
Parallaxing
Foreground objects
Shaders and 2D
Going further
Summary
5. NPCs and Interactions
Considering an RPG
Advanced programming techniques
Singletons and managers
The manager approach – using empty game objects
The singleton approach – using the C# singleton pattern
Communicating between game objects
Delegates
The configurable method pattern
The delegation pattern
Compound delegates
Events
Messaging
A better way
Background tasks and Coroutines
Enter Coroutines
IEnumerator
Yielding
Starting Coroutines
Closing the gap
Serialization and scripting
Saving and managing asset data
Using the serialized files in the editor
Accessing the .asset files in the code
Putting it all together
Getting the NPC's talking
The conversation object
Saving and serializing the object for later
The Conversation Component
Building a basic conversation system
The manager
Starting a conversation
Displaying the conversation
Adding more
Connecting the dots
Going further
Summary
6. The Big Wild World
The larger view
Types of maps
Fixed maps
Generated maps
In-game generated maps
2D doesn't mean you have to use only 2D
Going procedural
Screen space and world space
Putting it together
Adding the map
Adding places of interest
Creating the MapPoint prefabs for reuse
Updating the NavigationPrompt script
Updating the NavigationManager script
Traveling by click or touch
Managing input
Managing input priorities
Managing input order
Getting curvy
Fixing the start location
Traveling too far
Transitions
Updating level loading to use fading
Updating build settings to include new scenes
Going further
Summary
7. Encountering Enemies and Running Away
Event systems
Exploring randomness
Planning for random code/generation
True randomness
Basic Artificial Intelligence
State machines
Defining states
Simple singular choice
Planning for multiple cases
State managers
Sensors
Putting it together
Building the new scene
Adding the first enemy
Spawning the horde
Getting back home
The missing random piece
One last thing
Going further
Summary
8. Shopping for Weapons
Why do we shop?
The power of an item
Building your shop
Laying out your inventory
Rule of '99
Encumbrance system
Slot-based system
A mini game
Real world
Getting paid
Paid
Paid with trial
Ad supported
In-app purchase
In-game currency
Putting it together
Gathering the shop assets
Building the shop scene
Creating inventory items
Managing the shop
Adding 2D button behaviors
Updating the player inventory definition
Stocking the shop
Leaving the shop
Entering the shop
Managing your inventory
Ad...