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
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. |