Unity Design Patterns
C# game programming patterns, explained with interactive animations you can poke at — and production-style Unity code you can drop into your project.
Behavioral
How objects talk, react and hand off responsibility.
Command
Wrap an action in an object so it can be queued, logged, replayed or undone.
Mediator
Route all communication between peers through one coordinator so they never reference each other.
Memento
Capture an object’s state in a sealed snapshot so it can be restored later without exposing its internals.
Observer
Let one object broadcast changes to any number of listeners without knowing who they are.
State
Give each mode of an object its own class so behaviour changes cleanly when the mode changes.
Strategy
Swap out an algorithm at runtime by hiding each variant behind a common interface.
Creational
Controlled, flexible ways to bring objects into the world.
Singleton
Guarantee a class has exactly one instance and give everyone a single, well-known way to reach it.
Builder
Assemble a complex object one readable step at a time, then hand back the finished product.
Factory
Hide which concrete class gets created behind a single method that returns an interface.
Prototype
Create new objects by copying a configured example instead of building each one from scratch.
Structural
Composing classes and objects into larger shapes.
Adapter
Wrap an incompatible API so it looks exactly like the interface your game code already expects.
Composite
Treat a single object and a whole tree of objects through the same interface.
Decorator
Stack extra behaviour onto an object at runtime by wrapping it in layers that share its interface.
Facade
Give a tangle of subsystems one simple front door so the rest of the game only makes one call.
Proxy
Stand in for an expensive or sensitive object and control when, whether and how it is actually used.
Flyweight
Share the heavy data that many objects have in common, and keep only the small per-object differences on each instance.
Architectural
Wiring systems together without tangling them.
Dependency Injection
Hand objects the services they need from the outside instead of letting them find or build their own.
MVP
Split UI code into a dumb View, a pure-data Model and a Presenter that mediates between them.
Service Locator
Give game code one well-known registry to ask for services, so it never needs to know where they came from.
Game Programming
Patterns born in game engines: loops, components, data-driven design and Unity’s ScriptableObjects.
Game Loop
Advance the game in steady time steps, decoupled from how fast the machine can render frames.
Update Method
Give every entity a per-frame method and let the game loop call it, so each object simulates one frame of its own behaviour.
Component
Build entities by attaching small, single-purpose pieces of behaviour instead of growing a deep inheritance tree.
Type Object
Describe each kind of thing as a data object, so new kinds are assets instead of new subclasses.
Subclass Sandbox
Give a base class a small toolbox of protected operations and write every subclass only in terms of that toolbox.
Double Buffer
Read from a finished copy of the state and write into a second copy, then swap them, so an update never sees its own half-done work.
Event Queue
Separate the moment a request is sent from the moment it is handled, so bursts are buffered, merged and processed at a steady pace.
Event Bus
A global, typed publish/subscribe hub where publishers and subscribers share only event types, never references to each other.
ScriptableObject Events
Turn events into ScriptableObject assets, so scene objects and prefabs talk through a shared file wired in the Inspector instead of through each other.
ScriptableObject Variables
Store shared values like player health in assets, so writers and readers meet at the data instead of at each other.
Runtime Sets
Keep a live list of scene objects in a ScriptableObject that the objects maintain themselves, so nobody has to search the scene.
Optimization
Game-specific tricks for keeping frame times low.
Dirty Flag
Remember that derived data is out of date and recompute it only when someone actually needs it.
Object Pooling
Recycle a fixed set of objects instead of creating and destroying them, so spawning never stalls the frame.
Spatial Partition
Bucket objects by where they are so "what is near me?" only looks at the neighbourhood, not the whole world.
Data Locality
Lay data out in memory in the order your loop reads it, so the CPU cache can stream it instead of waiting on RAM.