Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


scenemanager

This is an old revision of the document!


SceneManager

When does scene loading happen? Scene loading and unloading happens at the start of the Update loop, within the EarlyUpdate phase of the PlayerLoop, and once during the initialization of the player, before the first Update loop.

Player Loop Phases:

  • Initialization (Scene Loading happens here)
  • Game Loop
    • Early Update
      • `UpdatePreloading` (Scene Loading happens here)
    • Update

The Unity scene flushing pipeline works differently in the initialization phase of the player. Scene loads thats are queued during initialization are blocked and fully loaded before the game starts. For example, if SceneManager.LoadSceneAsync is called in the Awake of the very first scene (ie. called before the first Update), then the engine will wait to fully load that scene as well, before starting the first Update.

The scene loading queue is a pipeline, and only one AsyncOperation can progress at a time. The Engine simply “flushes” the scene loading queue during initialization.

(The scene load pipeline is fully flushed) I don't yet know how it handles unloads if they are in the pipeline then, so if we need this behaviour we should debug and see how it works. If scene unloads get flushed too, we'll need to add a bit of logic for it, in the same way LoadSceneAsync does.

The scene loading pipeline only seems to load one scene, every other frame. That is to say if you have 3 small scenes in the pipeline (Scene1, Scene2, Scene3), loading additively, the engine will load them at the following times:

  • Frame N+0: Scene1
  • Frame N+1: —-
  • Frame N+2: Scene 2
  • Frame N+3: —-
  • Frame N+4: Scene 3

During a scene load (ie. Awake) the loading scene has scene.isLoaded=false. During a scene unload (ie. Destroy) the unloading scene has scene.isLoaded=false. 1)

Build Pipeline

Scenes are built during standalone in the following way: '

  1. The active scenes are closed.
  2. Each Scene in the BuildSettings is opened. At this point prefabs are still prefab instances.
  3. EditorSceneManager.sceneOpened callback is called.
  4. Prefabs in the scene are flattened (turned into normal game objects).
  5. IProcessScene and PostProcessScene callbacks are called.
  6. The final scene file is saved into the standalone build file.
  7. Repeat for next scene

Sample Scene Load (Editor)

Here is an example of a scene loading additively in the middle of a game, in the editor. Note that:

  • Prefabs.MergePrefabs (turning prefabs into gameobjects) happens before any other callback. (editor only)
  • Lightmapping is applied / loaded after Awake/OnEnable.
  • SceneManager.sceneLoaded happens after PostProcessScene;

API Notes:

SceneManager.sceneLoaded

  • Called *after* the loaded Scene Awakes.
1)
You might expect during the unload phase that isLoaded=true, but this is not the case
scenemanager.1613926406.txt.gz · Last modified: 2021/02/21 16:53 by 73.95.178.156