In this series of posts, I am going to describe an approach to architecting a game with a single scene. Follow along as I add to this series covering practical topics often missed in typical Unity tutorials.
Unity Single Scene Architecture Series
Introduction
Instead of multiple scenes, this architecture will rely on dynamically loading and unloading assets (such as prefabs) at runtime using the Unity Addressables package.
Additionally, the scene hierarchy will be used to organize and control complexity, manage state, and encourage dependency inversion (loose coupling). I'll also cover laying a good game foundation by including tests and a CI/CD pipeline as the project progresses.
Why?
There are so many different ways to build and structure games. Most simple games will load the main menu scene, and when playing load the first scene by build index. When that level is complete, load the next scene index, etc.
Larger games may use additive scene loading to split up larger levels, reduce load times and optimize memory usage. Assets can be loaded and unloaded at runtime, often with the Resources
folder (slow) or AssetBundles
(complex).
Mix this all together and it becomes confusing when to group objects in a scene, when to use prefabs/variants, and when to load at runtime.
This architecture seeks to simplify these decisions, reduce the need for singletons/static scope, and create a better iterative experience.
Caveats
If your game will rely heavily on baked lighting, then this architecture may not work for you. Unity does not have built in support for baking light with prefabs! I'll cover some options for lighting in a post as part of this series.
For now, let's forge ahead and dive in!
Project Setup
Create a new Unity project. I am using version 2021.3
with 3D core
features. Rename the SampleScene to "GameScene".
Use the Package Manager to install the Addressables
package.
Game Design
In Single Scenery the game will be as simple as possible to demonstrate the architecture. The player will move through each level, which will simply be a landscape composed of Unity's built-in 3D objects.
The player will move through the landscape, enjoy the scenery, and find a "key" that allows them to move to the next level.
Game Setup
We'll use our single scene to frame out the main game systems needed for Single Scenery.
With "Game Scene" open in the editor, let's mirror the above architecture with empty game objects. Create an empty game object in the root of the scene named "Game". Add empty child game objects named:
- Cameras
- Lights
- Input
- UI
- Player
- Levels
Drag "Directional Light" under "Lights". Drag "Main Camera" under "Cameras".
You should now have a scene hierarchy that looks like this:
Summary
We now have the bones of a game, a scene that is framed out with empty objects and ready to start implementing.
Next
Follow along to the next post in the series.
Unity Single Scene Architecture Series
Source Code: