Loading lessons...
Scene Management
Scene Management
In Unity, a Scene represents a single level, menu screen, or workspace. Your project is organized into scenes, and you load and unload them to navigate the game.
Loading Scenes in C#
To load and manage scenes, you must include the UnityEngine.SceneManagement namespace. You load a scene by its name or index:
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public void LoadNextLevel()
{
// Load scene named "Level2"
SceneManager.LoadScene("Level2");
}
}
Single vs Additive Loading
By default, loading a scene operates in Single Mode (LoadSceneMode.Single). This unloads the current scene and loads the new scene.
Alternatively, you can load scenes in Additive Mode (LoadSceneMode.Additive):
- The new scene is loaded alongside the current active scene(s).
- This is useful for splitting large worlds into smaller chunks, or loading a persistent UI scene on top of gameplay scenes.
Asynchronous Loading
Loading large scenes can freeze the game for a moment. To avoid this, load scenes asynchronously:
IEnumerator LoadSceneAsync(string sceneName)
{
AsyncOperation op = SceneManager.LoadSceneAsync(sceneName);
while (!op.isDone)
{
float progress = Mathf.Clamp01(op.progress / 0.9f);
Debug.Log("Loading progress: " + (progress * 100) + "%");
yield return null;
}
}
TL;DR
- Scenes are levels or screens stored as assets.
- Use
UnityEngine.SceneManagementnamespace to load scenes. - Single loading closes the current scene; Additive loading keeps it open.
- Use
LoadSceneAsyncto load scenes in the background without gameplay freeze.