Unity: code architecture¶
How to structure gameplay and application code so it stays changeable as the project grows.
Entries are sorted by score from 1 to 10 (criteria): 9–10 start here or strongly recommended, 6–8 recommended, 1–5 further reading or conditional.
Adopted references¶
- 10 Level up your code with design patterns and SOLID and its demo project: the baseline for patterns in Unity C#.
- 9 Create modular game architecture with ScriptableObjects (Unity 6 edition): the baseline for ScriptableObject-based architecture.
- 8 Game Programming Patterns (Robert Nystrom).
- 7 How to architect code as your project scales (Unity).
Key rules¶
Structure¶
- 8
UNITY-CODE-1Keep logic that doesn't need the engine in plain C# classes, with MonoBehaviours as thin adapters. The logic can then be unit tested without scenes. (Unity, How to architect code as your project scales, Humble Object pattern) - 7
UNITY-CODE-2Separate UI from game logic with Model-View-Presenter (or MVC). Views display state and forward input, and presenters hold no rendering code. (Unity e-book, Level up your code with design patterns and SOLID) - 4
UNITY-CODE-4Wrap player and AI actions in commands when you need undo, replay, rebinding or queuing. (Unity e-book, Level up your code with design patterns and SOLID, Nystrom, Command)
Data and ScriptableObjects¶
- 8
UNITY-CODE-6Don't use ScriptableObjects as save-game storage. Changes made at runtime persist in the Editor but not in a built player, so use a separate persistence mechanism. (Unity Manual, Save changes to ScriptableObject data) - 7
UNITY-CODE-5Put designer-tunable, shared data in ScriptableObject assets, not hard-coded values or copies on every prefab. (Unity, Separate game data and logic with ScriptableObjects, Unity Manual, Introduction to ScriptableObject) - 4
UNITY-CODE-7Use ScriptableObject-based enums when designers need to add values without code changes. (Unity, ScriptableObject-based enums)
Dependencies and communication¶
- 8
UNITY-CODE-8Make dependencies explicit through serialized references, constructor or method injection, or a DI container (for example VContainer), rather than singletons and scene-wideFindlookups everywhere. (Unity, Architect your code with ScriptableObjects, Unity Scripting API, GameObject.Find, VContainer, general: make dependencies explicit) - 8
UNITY-CODE-9Don't rely on the order in which Unity calls the same event function (Awake,Start, …) on different objects. Initialise in explicit phases or set Script Execution Order deliberately. (Unity Manual, Order of execution for event functions) - 7
UNITY-CODE-10Decouple systems with events. Use C# events or UnityEvents for local wiring, and ScriptableObject event channels between scenes and systems. (Unity e-book, Level up your code with design patterns and SOLID, Unity, ScriptableObject event channels, Unity Manual, UnityEvents)
Async and scale¶
- 6
UNITY-CODE-11For asynchronous code, useAwaitablewith async/await (Unity 2023.1+) or coroutines, and know which thread each continuation runs on. (Unity Manual, Asynchronous programming with Awaitable, Coroutines) - 5
UNITY-CODE-12For multiplayer, choose the netcode stack with the Multiplayer Center and study Boss Room before designing your own architecture. (Multiplayer Center, Netcode for GameObjects, Boss Room sample) - 5
UNITY-CODE-13Choose the UI system (UI Toolkit or uGUI) per use case using Unity's comparison. (Unity Manual, Comparison of UI systems, UI Toolkit for advanced Unity developers) - 4
UNITY-CODE-14Choose the Entities package (DOTS/ECS) deliberately for large simulations. It is a data-oriented architecture, not a drop-in optimisation for GameObject code. (Entities package manual, ECS samples)
Accessibility¶
- 6
UNITY-CODE-15For screen reader support, build an accessibility hierarchy with Unity's Accessibility module: anAccessibilityNodewith a label, role and state for each element that players read or use, set asAssistiveSupport.activeHierarchywhile that UI is shown, and kept in sync with the UI, including each node's screen position, in your own code. In Unity 6.3 LTS and later, the module drives TalkBack, VoiceOver and Narrator on Android, iOS, macOS and Windows, but it doesn't derive the hierarchy from the scene, so test with each target screen reader; on Android and iOS,AccessibilitySettingsalso reports the font scale, bold text and closed caption settings. This appliesA11Y-9andA11Y-12. (Unity Manual, Introduction to the Accessibility module, Unity Manual, Get started with screen reader support)