Cover
Contents at a Glance
Contents
About the Author
About the Technical Reviewer
Acknowledgments
Chapter 1: Unity Basics for 2D Games
Unity Projects, Assets, and Scenes
Project Wizard and Project Panel
Assets and Project Files
Scenes
Navigating Scenes and Viewports
GameObjects, Transforms, and Components
Cameras
Meshes and Geometry
Scripting and the Unity API
MonoDevelop
Components
Performance, Profiling, and the Stats Panel
The Profiler
The Stats Panel
Editor Add-Ons
Unity Interface Configuration
Summary
Chapter 2: Materials and Textures
Using Materials and Textures
Getting Started with Materials
Mesh Renderers
Shaders
Working with Textures
Materials for 2D Games
Method 1: Use White Ambient Light
Method 2: Use Light-Immune Shaders
Creating Textures
Rule #1: Power-2 Dimensions
Rule #2: Retain Quality
Rule #3: Expand Alpha Channels for Transparency
Creating Alpha Textures in Adobe Photoshop
Step 1. Separate Foreground From Background
Step 2. Duplicate and Defringe
Step 3. Expand Edges
Step 4. Merging and Alpha Channels
Creating Alpha Textures in GIMP
Importing Textures into Unity
Importing an Alpha Texture into Unity
Step 1. Import Alpha Texture
Step 2. Create Alpha Compliant Material
Step 3. Create a Textured Quad
Summary
Chapter 3: Quick 2D Workflow
Getting Started at Making “2D Alien Invasion”
Adding the Player and Enemies to the Scene
Implementing Player Movement
Implementing Player Weapons with Prefabs
Creating an Ammo Prefab Object
Implementing the Ammo Trajectory
Creating the Prefab Ammo Object
Defining the Cannon Point
Coding the Firing of Ammo
Implementing Moving Enemies and Collision
The EnemyController.cs Script
Setting the BoxCollider as a Trigger Volume
Adding a RigidBody Component
Adding a Level Background
Moving Forward and Project Limitations
Summary
Chapter 4: Customizing the Editor with Editor Classes
Editor Classes
Getting Started with Batch Rename
BatchRename.cs
Creating a Folder for Editor Extensions
Adding Batch Rename to the Application Menu
The CreateWizard Function
Testing the Batch Rename Menu Option
Reading Object Selections in the Scene
Making Use of Selection in BatchRename.cs
Testing Object Selections in Scene
Adding User Input to the Batch Rename Window
Completing the Batch Rename Feature
Summary
Chapter 5: Procedural Geometry and Textured Quads
Getting Started with the CreateQuad Feature
Setting the Quad’s Anchor Point
Specifying the Asset Path
Generating the Quad Mesh
Step 1—Create Vertices
Step 2—Create Quad as an Asset
Step 3—Instantiate Quad in Scene
Testing the Quad Mesh Generator
Summary
Chapter 6: Generating Atlas Textures
Getting Started with Atlas Textures
Configuring Texture Inputs
Atlas Textures and UVs
Generating an Atlas – Step 1: Optimizing Texture Inputs
Generating an Atlas – Step 2: Atlas Generation
Generating an Atlas – Step 3: Saving the Atlas Prefab
Testing the Atlas Texture
Summary
Chapter 7: UVs and Animation
Creating a Dockable Editor
Starting an Editor GUI — Selecting an Atlas
UV Editor—Adding an Input for the Atlas Prefab
Continuing with the GUI—Selecting a Texture
UVs and Manual Mode
Editing Mesh UVs
Putting It All Together—Finishing the UV Editor
Flipbook Animation
Summary
Chapter 8: Cameras and Pixel Perfection
Perspective versus Orthographic Cameras
World Units and Pixels
Position Objects
Field of View
Camera Size
Pixel Positioning
Pixel Perfection
Other Useful Camera Tricks
Depth
Advanced Depth
Assign Objects to Layers
Clearing the Depth Buffer
Summary
Chapter 9: Input for 2D Games
Detecting Mouse Clicks on Objects Automatically
Detecting Mouse Clicks on Objects Manually
Coding a Mouse Cursor
Working with Keyboard Input
Virtual Axes and Buttons—Input Abstraction
Mobile Input—Android, iOS, and Windows Phone
Detecting Touch Events on Mobiles
Mapping Touches to Mouse Input
Conditional Code Compilation
Summary
Chapter 10: Getting Started with a 2D Game
Game Design—Card Matching
Getting Started with the Game
Importing Assets
Generating an Atlas Texture
Generating Card and Cursor Quads
Assigning Materials and UVs
Camera and Resolution
Configuring the Scene—Setting Up the Cards
Setting Card Properties
Positioning and Arranging the Cards
Positioning and Arranging the Cards—Starting
Positioning and Arranging the Cards—Completing
The Game Manager
Shuffling the Cards
Handling User Input
Turn Management
Summary
Chapter 11: Completing the 2D Card Game
Networking in Unity — Getting Started
Establishing a Server
Establishing a Client
Testing Networked Functionality
Network Views
Making an Authoritative Server
Using RPCs
RPCs from Client to Server
Summary
Chapter 12: Optimization
Optimization—What Is It?
Tip #1—Minimize Vertex Count
Tip #2—Minimize Materials
Tip #3—Minimize UV Seams
Tip #4—Recycle Texture Space by Overlapping UVs
Tip #5—Use Per-Platform Texture Settings
Tip #6—Cache Components and Objects
Tip #7—Avoid Using Update
Tip #8—Minimize Colliders
Tip #9—Avoid OnGUI and the GUI class
Tip #10—Use Object Pooling
Tip #11—Use Static Batching
Tip #12—Utilize Skyboxes
Tip #13—Avoid Dynamic Lights and Shadows
Tip #14—Bake It; Don’t Simulate It
Summary
Chapter 13: Wrapping Things Up
GUIs and Resolution
Tiles and Tile-sets
Atlas Textures
Fonts
Unity Alternatives
Engines
Libraries and APIs
Asset Creation
Image Editing Software
Vector Graphic Software
2D Animation Software
Summary
Index