Unity SceneManager (C#) and Menu System
The scene manager controls the flow of the game between scenes. It is a core part of the framework discussed in my previous post as it is responsible for navigating between scenes, handling transitions, and more.
The scene manager prefab should be attached to the first scene, which is often a splash screen shown before the main menu screen. Since games often provide no way to view the splash screen except on game launch, this makes a great place to put an object that needs to persist throughout every level in the game.
SceneManager.cs
http://pastebin.com/JufJqtCc
There’s a lot going on here besides loading scenes and quitting the game. You’ll notice there are some game objects to be set within the scene manager for fading in and fading out scenes. I recommend not copying the scene manager script and instead just using the prefab that comes with the framework so you can just drop it in. Note the scene manager must be set within the splash scene (scene 0) of your game.
Functionality Explained
Menu items in particular need a game object with a load level function in order to work, but any script in the project can call the scene manager to load a level when needed. The scene manager can also be used to show loading screens, handle scene transitions, and more.
The class supports a basic progressive level loading system in the GoNextLevel function. The script is written in a way so that a scene is almost always referred to by name in the inspector instead of by number. This is because scene order my be moved around during development while scene names rarely change.
Keep in mind the developer is responsible for populating the levelNames string array with all levels that would be loaded progressively (in order). The order of levels indicating in the array is important, as that is the order in which levels loaded by the GoNextLevel function will be conducted. I believe having the developer indicate the array order in the inspector is more convenient than ordering scenes in a certain order in the scene manager (and is more friendly toward non-coders on the team).
An Out-Of-Box Menu System
The framework includes a menu system that comes ready-to-modify. The menu system as a whole works off of two main scripts – SceneManager.cs and MenuManager.cs
MenuManager.cs
http://pastebin.com/jkAkq0u0
The menu manager game object is a UI canvas that serves as the root of the menu system. Menu ‘screens’ are added as child objects parented to an empty game object transform, which is then parented to the menu manager game object. The setup basically looks like this:
Menu System (UI canvas object) MenuBG (UI image) Main Menu (empty game object) Menu Title (UI Text) Play (UI button) Options (UI button) Quit (UI button) Options (empty game object) Back (UI button)
The framework provides an example in the Menu.unity scene included, which I highly recommend as a basis. It’s important that the menu be contained in a single scene so resources do not have to be reloaded just to get to another part of the menu.
YouTube Overview and Demonstration
Here is a video where I give an overview of the framework as a whole and then demonstrate the basic usage and out-of-box functionality provided by the scene manager and menu system.
Up Next: ExtendedMonoBehavior.cs
The next framework class I will discuss is an extension of Monobehavior. There are a number of common functions and variable declarations every instance makes; most notably a reference to the object itself. By extending Monobehavior with these common declarations, we can shorten our scripts and developer time. See the script here: https://www.zesix.com/2015/02/unity-extendedmonobehavior-c/