Let's Talk about Scene Managment!

Sections: Scenes 101: Using Scenes: Scene and Level Managers:


What is a Scene?

    A scene is a unity run time data structure. However, I’m sure that description gave you about as much information as the 3 shells in your local bathroom. (For those of you who haven't seen demolition man, stop and do that now) So let's try this again, a scene in unity is essentially a level that the player is currently playing. If you were to start up unity right now and use the Ctrl + S hotkey for save file, it would ask you to save the current scene. The first time you do this the scene will be placed inside the games build settings under scenes. However, from that point on, scenes will have to be manually entered into the build settings.

How do I use Scenes?

    After you get done working with your current scene and you want to move on to another one, then you just need to save the scene and then select File->New Scene from the drop down menu. Once you have created your scene you will need to go to the build settings of your game and add the current scene to the scenes list by pressing the Add Open Scene button.

Level Managers and Scene Managers:

Scene Manger:

    The Scene Manger is a built in class provided by the unity code base under the SceneManager package. The documentation for which can be found here: Scene Manager Documentation. Developers can use the Scene Manger to script a game's current scene by passing a existing scene name or id to the Scene Manager’s Load Scene function.

//Load a Scene by name SceneManager.LoadScene(“Scene Name”); //Load a Scene by ID SceneManager.LoadScene(0);


Level Manger:

    The Level Manager is a script that I created that allows the game’s developer to transfer scenes dynamically by either using a GUI or by scripting the event. This script can be used by attaching the script to unity’s generated EventSystem gameobject and then setting the correct object, dunction, and parameters that are required to achieve the desired results. This is what the LevelManager script looks like in the C# programming language.

using UnityEngine; using UnityEngine.SceneManagement; public class LevelManager : MonoBehaviour { public void LoadSceneByName(string sceneName) { SceneManager.LoadScene(sceneName); } public void LoadSceneByID(int sceneID) { SceneManager.LoadScene(sceneID); } public void Quit() { Application.Quit(); } }

If you want to call the level manager script from another script you can use the following approach to do so. The script example below lives in a GameObject called Game Manager. I used this object as an observer entity that checks to see if an event is triggered and what actions should occur when an event is triggered.

using UnityEngine; using System.Collections; public class GameManager : MonoBehaviour { private LevelManager levelManager; //Used for initalizing objects upon game start void Start() { //This assumes that there is an object in the scene called LevelManager //with a LevelManager script attached. levelManager = GameObject.Find("LevelManager").GetComponent(); } void Update() { if(someCondition) { levelManager.LoadSceneByName("Scene Name"); } } }