Signup/Sign In

Scene Handling in Unity

To understand this we first have to realise what scenes are. Scenes refer to the objects which contain a particular scenario of the game.

Consider you have a game that has a world consisting of 2 parts: a city and a forest. Here we can say that this world can be defined in 2 scenes (though for larger games this may not be the case), one for the city and one for the forest.

Quoting from Unity Manuals:

Scenes contain the objects of your game. They can be used to create a main menu, individual levels, and anything else. Think of each unique Scene file as a unique level. In each Scene, you will place your environments, obstacles, and decorations, essentially designing and building your game in pieces.

Scince the few latest updates, Unity has started using a new library to access Scenes and traversing between them. This library is called the SceneManagement.

To use this library we have to first include it. We do this by writing

using UnityEngine.SceneManagement

at the start of the script.

In this library the class that you will encounter and use the most will be the SceneManager.


Including Scenes in Build

To be able to access a scene via the SceneManager class we first we need to add the scene to the Build Settings

To do this save the scene by CTRL+S. It will be saved in the Assets folder.

Now go to File in the menu bar and select Build Settings.

In the window that opens up, drag and drop the scene icon from the Project tab into the space under Scenes In Build.

We can add multiple scenes in a similar way and reorder them. The order in which the scenes are, decides their scene number and the scene which will load first when the game begins.

The Scene Manager

The Scene Manager class has the following:


Description:

Scene management at run-time.


Static Properties:

sceneCount The total number of currently loaded Scenes
sceneCountInBuildSettings Number of Scenes in Build Settings.

Static Methods:

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

Events:

activeSceneChanged Subscribe to this event to get notified when the active Scene has changed.
sceneLoaded Add a delegate to this to get notifications when a Scene has loaded.
sceneUnloaded Add a delegate to this to get notifications when a Scene has unloaded.

Courtesy: Unity Scene Manager