Cover....1
Title Page....2
Copyright and Credits....3
Dedication....4
Contributors....5
Table of Contents....7
Preface....12
Sections 1: Fundamentals....18
Chapter 1: Before We Begin....19
Notes about the new edition....19
The philosophy of the book....20
What are design patterns?....21
Which subjects aren't covered in this book?....21
The game project....22
Summary....23
Chapter 2: The Game Design Document....24
The design document....25
Game overview....25
Unique selling points....25
Minimum requirements....26
Game synopsis....26
Game objectives....27
Game rules....28
Game loop....28
Game environment....29
Camera, control, character (3Cs)....29
Camera....29
Character....30
Character description....30
Character metrics....31
Character states....31
Controller....32
Game ingredients....32
Superbikes....33
Pickups....33
Obstacles....34
Weaponry....34
Game systems....34
Game menu....36
Game HUD....36
Summary....37
Further reading....37
Chapter 3: A Short Primer to Programming in Unity....38
What you should already know....39
C# language features....39
Unity engine features ....43
Summary....45
Further reading....45
Section 2: Core Patterns....46
Chapter 4: Implementing a Game Manager with the Singleton....47
Technical requirements....48
Understanding the Singleton pattern....48
Benefits and drawbacks....50
Designing a Game Manager....50
Implementing the Game Manager....51
Testing the Game Manager....56
Summary....58
Chapter 5: Managing Character States with the State Pattern....59
Technical requirements....60
An overview of the State pattern....60
Defining character states....62
Implementing the State pattern....63
Implementing the State pattern....64
Testing the State pattern implementation....69
Benefits and drawbacks of the State pattern....70
Reviewing alternative solutions....72
Summary....73
Chapter 6: Managing Game Events with the Event Bus....74
Technical requirements....75
Understanding the Event Bus pattern....75
Benefits and drawbacks of the Event Bus pattern....77
When to use the Event Bus....78
Managing global race events....78
Implementing a Race Event Bus....79
Testing the Race Event Bus....81
Reviewing the Event Bus implementation....86
Reviewing some alternative solutions....87
Summary....87
Chapter 7: Implement a Replay System with the Command Pattern....88
Technical requirements....89
Understanding the Command pattern....89
Benefits and drawbacks of the Command pattern....92
When to use the Command pattern....92
Designing a replay system....93
Implementing a replay system....95
Implementing the replay system....95
Testing the replay system....99
Reviewing the implementation....103
Reviewing alternative solutions....104
Summary....104
Chapter 8: Optimizing with the Object Pool Pattern....105
Technical requirements....106
Understanding the Object Pool pattern....106
Benefits and drawbacks of the Object Pool pattern....107
When to use the Object Pool pattern....108
Implementing the Object Pool pattern....109
Steps for implementing the Object Pool pattern....109
Testing the Object Pool implementation....114
Reviewing the Object Pool implementation....116
Reviewing alternative solutions....116
Summary....116
Chapter 9: Decoupling Components with the Observer Pattern....117
Technical requirements....118
Understanding the Observer pattern....118
Benefits and drawbacks of the Observer pattern....119
When to use the Observer pattern....120
Decoupling core components with the Observer pattern....120
Implementing the Observer pattern....122
Testing the Observer pattern implementation....128
Reviewing alternative solutions....130
Summary....130
Chapter 10: Implementing Power-Ups with the Visitor Pattern....131
Technical requirements....132
Understanding the Visitor pattern....132
Benefits and drawbacks of the Visitor pattern....134
Designing a power-up mechanic....135
Implementing a power-up mechanic....136
Implementing the power-up system....136
Testing the power-up system implementation....142
Reviewing the power-up system implementation....144
Summary....145
Chapter 11: Implementing a Drone with the Strategy Pattern....146
Technical requirements....147
Understanding the Strategy pattern....147
Benefits and drawbacks of the Strategy pattern....148
When to use the Strategy pattern....149
Designing an enemy drone....150
Implementing an enemy drone....152
Steps to implementing an enemy drone....152
Testing the enemy drone implementation....157
Reviewing the enemy drone implementation....159
Reviewing alternative solutions....159
Summary....160
Chapter 12: Using the Decorator to Implement a Weapon System....161
Technical requirements....161
Understanding the Decorator pattern....162
Benefits and drawbacks of the Decorator pattern....163
When to use the Decorator pattern....164
Designing a weapon system....165
Implementing a weapon system....166
Implementing the weapon system....166
Testing the weapon system....173
Reviewing the weapon system....176
Reviewing alternative solutions....177
Summary....177
Chapter 13: Implementing a Level Editor with Spatial Partition....178
Technical requirements....179
Understanding the Spatial Partition pattern....179
When to use the Spatial Partition pattern....181
Designing a level editor....181
Implementing a level editor....184
Steps for implementing a level editor....184
Using the level editor....190
Reviewing the level-editor implementation....190
Reviewing alternative solutions....190
Summary....191
Section 3: Alternative Patterns....192
Chapter 14: Adapting Systems with an Adapter....193
Technical requirements....194
Understanding the Adapter pattern....194
Benefits and drawbacks of the Adapter pattern....196
When to use the Adapter pattern....197
Implementing the Adapter pattern....198
Implementing the Adapter pattern....198
Testing the Adapter pattern implementation....201
Summary....202
Chapter 15: Concealing Complexity with a Facade Pattern....203
Technical requirements....203
Understanding the Facade pattern....204
Benefits and drawbacks....205
Designing a bike engine....206
Implementing a bike engine....207
Testing the engine facade....212
Reviewing alternative solutions....213
Summary....213
Chapter 16: Managing Dependencies with the Service Locator Pattern....214
Technical requirements....215
Understanding the Service Locator pattern....215
Benefits and drawbacks of the Service Locator pattern....217
When to use the Service Locator pattern....217
Implementing a Service Locator pattern....218
Testing the Service Locator pattern....222
Reviewing alternative solutions....223
Summary....224
About Packt....225
Other Books You May Enjoy....226
Index....229
While writing this book, I realized that it was almost impossible to satisfy every potential reader's needs when it comes to game development. Game development is a diverse industry with many different platforms and genres, and each one has its own unique characteristics that cannot be covered in a single book. Therefore, I decided to focus on a specific audience: game programmers who are working on mobile or indie games using the Unity engine. These programmers are in the process of refactoring their code to make it more maintainable and scalable, and they have a basic understanding of the Unity engine and the C# language. Senior programmers working on larger-scale games may find some examples in this book to be limited, but they will still find valuable insights into the challenges they face in their own projects.Daily, however, the content of this book may offer another perspective on the use of design patterns in Unity. Therefore, feel free to skip any chapter if you are already familiar with the theory and would like to see how I have implemented a specific pattern.