class in UnityEngine.SceneManagement
/
Implemented in:UnityEngine.CoreModule
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.
CloseFor 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.
CloseManage 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
Scene management in the Editor
Notes
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()); } }
loadedSceneCount | The number of loaded Scenes. |
sceneCount | The current number of Scenes. |
sceneCountInBuildSettings | Number of Scenes in Build Settings. |
CreateScene | Create an empty new Scene at runtime with the given name. |
GetActiveScene | Gets the currently active Scene. |
GetSceneAt | Get the Scene at index in the SceneManager's list of loaded Scenes. |
GetSceneByBuildIndex | Get a Scene struct from a build index. |
GetSceneByName | Searches through the Scenes loaded for a Scene with the given name. |
GetSceneByPath | Searches all Scenes loaded for a Scene that has the given asset path. |
LoadScene | Loads the Scene by its name or index in Build Settings. |
LoadSceneAsync | Loads the Scene asynchronously in the background. |
MergeScenes | This will merge the source Scene into the destinationScene. |
MoveGameObjectsToScene | Move multiple GameObjects, represented by a NativeArray of instance IDs, from their current Scene to a new Scene. |
MoveGameObjectToScene | Move a GameObject from its current Scene to a new Scene. |
SetActiveScene | Set the Scene to be active. |
UnloadSceneAsync | Destroys all GameObjects associated with the given Scene and removes the Scene from the SceneManager. |
activeSceneChanged | Subscribe to this event to get notified when the active Scene has changed. |
sceneLoaded | Assign a custom callback to this event to get notifications when a Scene has loaded. |
sceneUnloaded | Add a delegate to this to get notifications when a Scene has unloaded. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.