Table of Contents
Preface vii
1 Working with Unity 1
1.1 Getting Around in Unity 1
1.2 Working with Game Objects 10
1.3 Working with Components 11
1.4 Working with Prefabs 15
1.5 Working with Scenes 16
1.6 Managing Assets 17
1.7 Building Unity Projects 18
1.8 Accessing Preferences 19
2 Scripting 21
2.1 Working with MonoBehaviours 21
2.2 Using Callbacks 26
2.3 Creating Frame Rate-In dependent Behavior 28
2.4 Accessing Components 29
2.5 Finding Objects 31
2.6 Using Coroutines 32
2.7 Singletons 35
2.8 Loading a Level (Scene) 38
2.9 Storing Data on Disk 42
2.10 Saving and Loading the Game State 42
2.11 Managing Objects Using an Object Pool 58
2.12 Storing Data in Assets Using ScriptableObject 66
3 Input 69
3.1 Working with Keyboard Input 69
3.2 Working with Mouse Input 70
3.3 Locking and Hiding the Mouse Cursor 71
3.4 Working with Gamepads 72
3.5 Customizing Unity's input System 74
3.6 Responding to Pointer Events from the Event System 78
4 Math 83
4.1 Storing Coordinates of Varying Dimensions Using Vectors 84
4.2 Rotating in 3D Space 89
4.3 Performing Transformations in 3D Space with Matrices 91
4.4 Working with Angles 95
4.5 Finding the Distance to a Target 97
4.6 Finding the Angle to a Target 98
5 2D Graphics 101
5.1 Importing Sprites 101
5.2 Adding a Sprite to the Scene 103
5.3 Creating a Sprite Animation 104
5.4 Creating a Sprite with 2D Physics 105
5.5 Customizing Sprite Collision Shapes 105
5.6 Using a Composite Collider 108
5.7 Using the Sprite Packer 110
5.8 Applying Forces to 2D Objects 110
5.9 Creating a Conveyor Belt 112
5.10 Using a Custom Material for Sprites 114
5.11 Managing Sprite Sorting 116
5.12 Using Sorting Groups 117
5.13 Creating a 2.5D Scene 119
6 3D Graphics 121
6.1 Creating a Simple Material 121
6.2 Controlling a Materials Property Through a Script 123
6.3 Creating an Unlit Material 125
6.4 Setting Up a Material Using Textures 125
6.5 Making a Material Use a Shader 128
6.6 Setting Up a Bloom Effect Using Post-Processing 128
6.7 Using High-Dynamic-Range Colors 133
6.8 Setting Up a Project to Use a Scriptable Render Pipeline 136
6.9 Creating a Shader Using the Shader Graph 138
6.10 Creating a Glowing Effect Using the Shader Graph 140
6.11 Exposing Properties from a Shader Graph 142
6.12 Animating a Shader over Time 144
6.13 Controlling the Speed of an Animated Shader 146
6.14 Using a Subgraph to Reuse Graph Components 147
6.15 Implementing a Dissolve Effect Using a Shader Graph 149
6.16 Using Baked Lighting and Real-Time Lighting 152
6.17 Using Baked Emission Sources 154
6.18 Making Static Objects Cast Shadows on Dynamic Objects 155
6.19 Using Light Probes to Influence Lighting 156
6.20 Using Reflection Probes 159
6.21 Faking a Dynamic Emissive Object 163
6.22 Loading Textures from Disk 164
6.23 Rendering to a Texture 166
6.24 Saving Screenshots 169
7 Physics and Character Control 171
7.1 Understanding Fixed Update 171
7.2 Implementing Mouselook 172
7.3 Controlling a 3D Character 175
7.4 Interacting with Switches and Objects 178
7.5 Picking Up and Putting Down Objects 181
7.6 Detecting When an Object Is Touching Another Object 187
7.7 Detecting When an Object Is in a Trigger Area 188
7.8 Implementing Moving Platforms 190
7.9 Implementing Platform Riding 193
7.10 Responding to Being Pushed by Objects 197
8 Animation and Movement 201
8.1 Animating an Object 202
8.2 Basic Character Movement 206
8.3 Inverse Kinematics 211
8.4 Masked Movement 215
8.5 Blended Movement 220
8.6 Navigation and Animating in Sync 224
8.7 Cinematic Camera Tracking 228
8.8 Automatically Switching Cameras 231
8.9 Keeping Multiple Objects in View 232
8.10 Dollying a Camera 233
9 Gameplay 237
9.1 Managing Quests 237
9.2 Managing Hitpoints 253
9.3 Creating a Top-Down Camera 256
9.4 Dragging a Box to Select Objects 259
9.5 Creating a Menu Structure 263
9.6 Creating a Wheeled Vehicle 267
9.7 Keeping a Car from Tipping Over 271
9.8 Creating Speed Boosts 272
9.9 Creating a Camera That Orbits Around Its Target 274
9.10 Creating Orbiting Cameras That Won't Clip Through Walls 277
9.11 Detecting When the Player Has Completed a Lap 278
10 Behavior and AI 289
10.1 Defining a Path That AI Entities and the Player Can Follow 289
10.2 Letting Entities in Your Game Follow a Path 291
10.3 Enemies Detecting When They Can See the Player 293
10.4 Finding a Good Distribution of Random Points (Poisson Disc) 299
10.5 Enemies Detecting Where They Can Take Cover 304
10.6 Building and Using a State Machine 310
11 Sound and Music 315
11.1 Playing Sounds 315
11.2 Setting Up a Mixer 318
11.3 Using Audio Effects 320
11.4 Using Send and Receive Effects 322
11.5 Ducking 325
11.6 Using Multiple Audio Zones 326
11.7 Playing Audio Through Code 328
11.8 Using a Sound Manager 329
12 User Interface 335
12.1 Working with UI Controls 336
12.2 Theming Controls 340
12.3 Animating the UI 344
12.4 Creating a List of Items 347
12.5 Fading Out List Items 352
12.6 Creating Onscreen Position Indicators 354
12.7 Custom Editors 359
12.8 Property Drawers 364
12.9 Attribute Drawers 369
12.10 Asset Processing 372
12.11 Scripted Importers 374
12.12 Wizards 378
Index 381