liveBook · Manning

Singleton pattern

As explained in High Performance Unity Game Development

A singleton is a pattern that restricts a class to one shared instance and provides centralized access to it.

A game with several menus and a gameplay board needs one place to coordinate what runs and what appears on screen. A singleton gives those Unity objects access to the same controller: place one Game object in the scene and call Game.Instance from a menu or the board. section 5.1.1

Game drives the game loop, holds GameData and Balance, and manages menu initialization, visibility, and Tick() calls. For a menu change, it asks Logic to update MenuState, then hides the old menu and shows the new one. section 5.1.1

Calling the shared controller

When gameplay ends, a Unity-facing object can call `if (gameOver) Game.Instance.GameOver();`. The call reaches the same Game object used by the menus and board. section 5.1.1

Keep the convenient access within clear boundaries:

  • Menus and the board may call Game because they are Unity objects. section 5.1.1
  • Logic functions should not call the singleton or read hidden global state. Pass in the data they need and return results, so gameplay logic remains independent and testable. section 5.1.1
  • If assets come from bundles, the resources folder, and addressables, use separate source-specific asset-manager singletons. That makes an asset’s origin easier to identify and debug. section 7.2.3
Figure 5.3 The game loop with a single script, Game, controlling everything. Now it’s trivial for us to figure out what code is running and when, because we are controlling the flow in the game loop, instead of relying on Unity, a black box, to control it for us.

The diagram contrasts a single Game-controlled loop with relying on scattered Unity lifecycle callbacks to determine execution order. section 5.1.1

How High Performance Unity Game Development covers it

How deep it goes
Defines shared access and explains menu transitions and logic boundaries, but does not provide instance-enforcement code.
Examples
Shows a Game.Instance game-over call and separate asset-manager singletons for bundles, resources, and addressables.
What it assumes
Assumes familiarity with Unity scenes, menus, and lifecycle callbacks while defining singleton access directly.
What you can do after
Apply it via Unity’s Game.Instance to reach a shared controller while keeping gameplay logic independent of it.
How it connects
Connects the singleton pattern to game loops, menu state, data-oriented design, and asset management.

Also explained in

Compare how 7 books explain Singleton pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of High Performance Unity Game Development. The section links point to the parts of the book it draws on.