Source: Level Up Your Code with Game Programming Patterns (Unity 2022)

Source metadata

  • Type: E-book / technical guide
  • Author: Unity Technologies
  • Year: 2022
  • Length: ~5,090 lines extracted

Key takeaways

  • The e-book applies classic Gang of Four (GoF) design patterns to Unity and C# game development, with working code examples throughout.
  • SOLID principles are presented as foundational before any pattern is introduced — the book frames patterns as applications of SOLID rather than alternatives.
  • The Singleton pattern is given a generic Singleton<T> : MonoBehaviour implementation that handles lazy instantiation and DontDestroyOnLoad automatically.
  • The Command pattern is demonstrated with an undo stack (CommandInvoker + Stack<ICommand>), which is directly applicable to level editors, move histories, and replay systems.
  • The State pattern is built around an IState interface (Enter, Update, Exit) and a StateMachine controller — this is the OOP alternative to Unity’s Animator state machines.
  • The Observer pattern is shown with plain C# events (public event Action ThingHappened) as well as Unity-specific UnityEvent wiring. Both approaches are explained with trade-offs.
  • The Factory pattern uses a base interface or abstract class so the calling code never references concrete types — keeps instantiation logic centralised.
  • Object Pool is presented as a CPU/GC optimisation for frequently spawned/destroyed objects (bullets, particles). The pattern pre-allocates a pool and returns objects rather than destroying them.
  • MVP (Model-View-Presenter) is introduced as the structural pattern most suited to Unity’s inspector-driven workflow, as a preferred alternative to MVC.

Notable claims

“Avoid getting into the habit of making everything a Singleton. It can be useful in some contexts, but overuse leads to code that is difficult to reason about and test.” (lines ~2900–2960)

“Events notify your observers automatically without the subject needing to know who is listening.” (lines ~4090–4100)

“Using the Command pattern, you can store a sequence of actions in a queue or a stack. This enables undo/redo operations and replay functionality.” (lines ~3159–3165)

“UnityEvents allow non-programmers to set up event-driven behaviours directly in the Inspector, but carry a performance overhead compared to C# delegates.” (lines ~4450–4507)

“Object pooling is an optimisation technique to relieve the CPU when creating and destroying objects in large numbers.” (lines ~2370–2380)

Relevance

This source is the primary reference for pattern-specific pages in wiki/programming/unity-architecture-patterns/. It directly informs:

Open questions raised

  • When does the State pattern (OOP IState) become preferable to Unity’s built-in Animator state machine? The book implies OOP states suit complex gameplay logic, but gives no clear threshold.
  • The book recommends MVP over MVC for Unity — is this still the consensus in larger Unity projects with UI Toolkit?
  • Object pools are noted to reduce garbage collection stuttering. How does this interact with Unity’s Addressables or PooledObject from Unity’s own ObjectPool<T> API (added in Unity 2021)?