Version: Unity 6.1 Alpha (6000.1)
LanguageEnglish
  • C#

SceneManager

class in UnityEngine.SceneManagement

/

Implemented in:UnityEngine.CoreModule

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Manage scenes in the Player and in Play mode in the Editor.

You can use the SceneManager to manage and manipulate scenes in the Player.

Scene creation, loading and unloading

Accessing loaded scenes

The Scene Manager offers APIs to access currently loaded Scenes. For example, SceneManager.loadedSceneCount, SceneManager.GetSceneAt, and SceneManager.GetSceneByPath.

Scene manipulation

To move objects between scenes, use methods like SceneManager.MergeScenes and SceneManager.MoveGameObjectToScene.

SceneManager events

The SceneManager also exposes the following events:

Scripts can register on these events and then be notified when there are changes in the state of the SceneManager.

The scene list

The Player contains a BuildSettings object which records the list of scenes that are available to load. The contents of this list is exposed by SceneManager.sceneCountInBuildSettings and SceneUtility.GetScenePathByBuildIndex.

The contents of this list is determined when the Player is built:

The scene order is crucial for several reasons:

AssetBundles and Scenes

  • Additional scenes can be included in AssetBundles. When an AssetBundle that contains scenes is loaded, its scenes become available to the SceneManager and can be loaded by path using SceneManager.LoadSceneAsync or SceneManager.LoadScene.
  • Scenes from AssetBundles have a Scene.buildIndex of -1.
  • When loading scenes by path, a match from loaded AssetBundle takes priority over scenes in the Player build.

Scene management in the Editor

  • Use EditorSceneManager instead of SceneManager for scene authoring and manipulation in the Editor.
  • The SceneManager API should only be used in Play mode. In Edit mode calls to unsupported methods such as SceneManager.LoadSceneAsync will throw an invalid operation exception.
  • In Play mode, only scenes listed in EditorBuildSettings are available to load, along with scenes from loaded AssetBundles, simulating Player behavior.

Notes

  • Loading Scenes by index can be fragile due to potential reordering; the recommended best practice is to load scenes by path for better clarity.
  • Loading scenes by filename (without a full path) can cause issues if multiple scenes share the same name; full path specification removes that ambiguity.

Additional resources: EditorSceneManager, SceneUtility, Scene.buildIndex, EditorBuildSettingsScene.enabled, AssetBundle.GetAllScenePaths.

using UnityEngine;
using UnityEngine.SceneManagement;

// This MonoBehaviour could be placed as a component inside the first scene in the Build Profiles Scene List. // When the Player starts it instantiates this MonoBehaviour, which in turn loads // an additional scene. public class SceneLoader : MonoBehaviour { // This scene must be listed in the Scene List in the Build Profiles Window, // or available from a loaded AssetBundle. const string sceneToLoad = "Assets/Example/AnotherScene.unity";

void Start() { var op = SceneManager.LoadSceneAsync(sceneToLoad, LoadSceneMode.Additive); op.completed += (AsyncOperation obj) => { Scene loadedScene = SceneManager.GetSceneByPath(sceneToLoad); Debug.Log($"{sceneToLoad} finished loading (build index: {loadedScene.buildIndex})."); Debug.Log($"It has {loadedScene.rootCount} root(s)."); Debug.Log($"There are now {SceneManager.loadedSceneCount} Scenes open."); }; }

private void OnDestroy() { // When closing the Scene containing this MonoBehaviour we also remove the Scene we loaded SceneManager.UnloadSceneAsync(sceneToLoad); } }
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneInfo : MonoBehaviour { void Start() { LogSceneManagerState(); }

void LogSceneManagerState() { var sb = new StringBuilder(); sb.AppendLine("SceneManager state");

sb.AppendLine($"Active Scene: {SceneManager.GetActiveScene().path}");

sb.AppendLine($"Scene List (size {SceneManager.sceneCountInBuildSettings})"); for(int i = 0; i < SceneManager.sceneCountInBuildSettings; i++) { var scenePath = SceneUtility.GetScenePathByBuildIndex(i); sb.AppendLine($" {i}: {scenePath}"); }

sb.AppendLine($"Loaded Scenes (size {SceneManager.sceneCount})"); for(int i = 0; i < SceneManager.sceneCount; i++) { var scene = SceneManager.GetSceneAt(i); sb.AppendLine($" {i}: {scene.path}"); }

Debug.Log(sb.ToString()); } }

Static Properties

loadedSceneCountThe number of loaded Scenes.
sceneCountThe current number of Scenes.
sceneCountInBuildSettingsNumber of Scenes in Build Settings.

Static Methods

CreateSceneCreate an empty new Scene at runtime with the given name.
GetActiveSceneGets the currently active Scene.
GetSceneAtGet the Scene at index in the SceneManager's list of loaded Scenes.
GetSceneByBuildIndexGet a Scene struct from a build index.
GetSceneByNameSearches through the Scenes loaded for a Scene with the given name.
GetSceneByPathSearches all Scenes loaded for a Scene that has the given asset path.
LoadSceneLoads the Scene by its name or index in Build Settings.
LoadSceneAsyncLoads the Scene asynchronously in the background.
MergeScenesThis will merge the source Scene into the destinationScene.
MoveGameObjectsToSceneMove multiple GameObjects, represented by a NativeArray of instance IDs, from their current Scene to a new Scene.
MoveGameObjectToSceneMove a GameObject from its current Scene to a new Scene.
SetActiveSceneSet the Scene to be active.
UnloadSceneAsyncDestroys all GameObjects associated with the given Scene and removes the Scene from the SceneManager.

Events

activeSceneChangedSubscribe to this event to get notified when the active Scene has changed.
sceneLoadedAssign a custom callback to this event to get notifications when a Scene has loaded.
sceneUnloadedAdd a delegate to this to get notifications when a Scene has unloaded.