SceneManager

Static facade wrapping Unity's SceneManager with dependency-aware scene loading. Resolves dependency chains from the SceneGraph and loads prerequisite scenes automatically before the target scene. Supports both synchronous and async loading, and fires events for scene load/unload.

Definition

Namespace: Paragon.Core.SceneManagement Assembly: Paragon.dll

public class SceneManager

Remarks

SceneManager is the primary API for scene operations in the Paragon framework. It wraps Unity's UnityEngine.SceneManagement.SceneManager and adds dependency chain resolution via the SceneGraph.

Initialization

The class auto-initializes via [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]. In non-editor builds, it discovers the SceneGraph via Resources.FindObjectsOfTypeAll<SceneGraph>(). It also subscribes to Unity's sceneLoaded and sceneUnloaded callbacks to translate them into Paragon Scene-typed events.

Dependency Chain Loading

When loadDependencies is true (the default), LoadScene and LoadSceneAsync call GetDependencyChain() to perform an iterative depth-first search (DFS) that produces a topologically ordered list of dependencies. Dependencies are loaded before the target scene.

The DFS algorithm:

  1. Pushes the target scene's node onto a stack

  2. For each node, pushes unvisited dependencies (in reverse order) onto the stack

  3. When a node has no more unvisited dependencies, it is popped and added to the result set

  4. This produces a post-order traversal — dependencies appear before dependents

Load Scene Mode

After loading the first dependency (or the target scene if there are no dependencies), the mode is forced to ADDITIVE for all subsequent loads in the chain. When the original mode is SINGLE, the target scene is set as the active scene once loading completes.

Quick Lookup

Goal
How

Load scene (sync)

SceneManager.LoadScene(scene)

Load scene (async)

await SceneManager.LoadSceneAsync(scene)

Unload scene (sync)

SceneManager.UnloadScene(scene)

Unload scene (async)

await SceneManager.UnloadSceneAsync(scene)

Load with dependencies

SceneManager.LoadScene(scene, loadDependencies: true) (default)

Load without dependencies

SceneManager.LoadScene(scene, loadDependencies: false)

Load additively

SceneManager.LoadScene(scene, loadSceneMode: LoadSceneMode.ADDITIVE)

Get active scene

SceneManager.GetActiveScene()

Set active scene

SceneManager.SetActiveScene(scene)

Check if loaded

SceneManager.IsSceneLoaded(scene)

Get all loaded scenes

SceneManager.GetLoadedScenes()

Look up scene by name

SceneManager.GetScene("MyScene")

Look up scene by path

SceneManager.GetScene("Assets/Scenes/MyScene.unity")

Move object to scene

SceneManager.MoveGameObjectToScene(go, scene)

Move object to active scene

SceneManager.MoveGameObjectToActiveScene(go)

Listen for loads

SceneManager.SceneLoaded += handler

Listen for unloads

SceneManager.SceneUnloaded += handler

Fields

Field
Type
Access
Description

sceneGraph

SceneGraph

protected static

The scene graph used for lookups and dependency resolution

Events

Event
Type
Description

SceneLoaded

Action<Scene>

Fired after a scene finishes loading (maps from Unity's sceneLoaded callback)

SceneUnloaded

Action<Scene>

Fired after a scene is unloaded (maps from Unity's sceneUnloaded callback)

Methods

LoadScene

Loads a scene synchronously with optional dependency chain resolution.

Parameter
Type
Default
Description

scene

Scene

The target scene to load

loadDependencies

bool

true

Whether to load dependency scenes first

loadSceneMode

LoadSceneMode

SINGLE

SINGLE unloads current scenes; ADDITIVE keeps them

Behavior:

  1. If loadDependencies is true, resolves the dependency chain (excluding the target) and loads each in order

  2. After loading dependencies, forces mode to ADDITIVE for subsequent loads

  3. Loads the target scene

  4. If original mode was SINGLE, subscribes to SceneLoaded to call SetActiveScene when the target finishes loading

LoadSceneAsync

Loads a scene asynchronously with optional dependency chain resolution. Returns a Task that completes when all scenes (dependencies + target) finish loading.

Parameter
Type
Default
Description

scene

Scene

The target scene to load

loadDependencies

bool

true

Whether to load dependency scenes first

loadSceneMode

LoadSceneMode

SINGLE

SINGLE unloads current scenes; ADDITIVE keeps them

Behavior:

  1. Starts all dependency load operations and collects AsyncOperation handles

  2. Starts the target scene load operation

  3. Awaits all operations via Yield.WaitUntil(() => all.isDone)

  4. If original mode was SINGLE, sets the target as the active scene

UnloadScene

Unloads a scene. Wraps UnloadSceneAsync with async void.

Parameter
Type
Default
Description

scene

Scene

The scene to unload

unloadDependencies

bool

false

Whether to also unload dependency scenes

UnloadSceneAsync

Unloads a scene asynchronously. Returns a Task that completes when all unload operations finish.

Parameter
Type
Default
Description

scene

Scene

The scene to unload

unloadDependencies

bool

false

Whether to also unload dependency scenes

GetDependencyChain

Performs an iterative DFS to compute the full dependency chain for a scene, in topological order (dependencies first).

Parameter
Type
Description

scene

Scene

The root scene to resolve dependencies for

Returns: An IEnumerable<Scene> containing all dependencies plus the target scene, in load order. The target scene appears last.

MoveGameObjectToScene

Moves a GameObject to the specified scene.

MoveGameObjectToActiveScene

Moves a GameObject to the currently active scene.

GetActiveScene

Returns the currently active Scene.

SetActiveScene

Sets a loaded scene as the active scene.

GetLoadedScenes

Returns all scenes from the SceneGraph that are currently loaded.

IsSceneLoaded

Checks if a scene is currently loaded and valid.

GetScene(string)

Looks up a scene by name or path. If the string contains '/', it matches by path; otherwise by name.

Returns: The matching Scene, or null if not found.

GetScene(UnityScene)

Looks up a Paragon Scene from a Unity scene, using the Unity scene's path.

TryGetScene

Attempts to look up a scene by name or path.

Returns: true if the scene was found in the SceneGraph.

GetUnityScene

Converts a Paragon Scene to a Unity Scene by looking up the path.

Extension Points

Subclassing SceneManager

The sceneGraph field is protected static, allowing subclasses to provide custom graph initialization:

The GetDependencyChain method is protected static, making it available to subclasses for custom loading logic.

Enums

LoadSceneMode

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation
circle-exclamation
circle-exclamation

Examples

Load a Level with Dependencies

Async Scene Transition

Listening to Scene Events

Checking Scene Status

See Also

Last updated