Scene Reference
A Scene Reference has the role of representing a Scene at runtime, with not much more refinement than a string literal. The main difference from a classic string is how a Scene Reference allows to use Scene Assets to assign to the field, and thus how it looks like in the inspector. It also integrates with the Startup Scene Mechanism if used in conjunction with the ScenesSystem.
A SceneReference field is defined as usual:
[SerializeField]
private SceneReference _sceneReference;
In the Inspector, it looks like this:
The status indicator has four possible states based on the properties of the referenced Scene.
| Aspect | Meaning | Example |
|---|---|---|
| The referenced Scene is null or not present in the active Scenes List. | ||
| The referenced Scene is present in the active Scenes List but it's disabled. | ||
| The referenced Scene is present in the active Scenes List. | ||
| The referenced Scene is an Addressable and will be loaded using Addressables. |
Customization Attributes
The Type Reference inspector can be further customized by decorating the field with attributes.
Optional
A Scene Reference decorated with the Optional won't show the status indicator if the scene is not assigned, since a null value becomes valid.
[SerializeField, Optional]
private SceneReference _sceneReference;
Allow Editor Override
A Scene Reference decorated with the AllowEditorOverride will add a toggle ( /
) to the field used to enable or disable an editor override.
[SerializeField, AllowEditorOverride]
private SceneReference _sceneReference;
If enabled () and if the Scene Reference is used to load a Scene through the
ScenesSystem, the serialized value will be ignored and the Scene opened before entering playmode is loaded instead. For example, the following code:
[SerializeField, AllowEditorOverride]
private SceneReference _firstScene;
private IEnumerator Start()
{
yield return Systems.Load();
ScenesSystem.Instance.LoadScene(_firstScene);
}
If used in conjunction with the Startup Scene settings and when playing in the editor, this code allows to swap the normal _firstScene to be loaded with the one that's being edited outside of play mode. For instance:
Let's suppose _firstScene is a reference to a Scene called MainMenu and that the Scene Level 14 is being edited and needs to be tested.
Entering playmode with _firstScene override enabled will load the Systems and then load Level 14 instead of MainMenu, which can thus be tested in isolation but with full support of the Systems.