Graphics Reference
In-Depth Information
4.10 Scene Manager
The scene manager deals with loading and saving different scenes. It is intended to be used
for loading different levels in a game, but also it may easily be used for loading the menu
if required.
The example game Interstellar Paranoids uses it to load game levels. It is attached to an
empty gameObject in the menu scene, and thanks to its call to DontDestroyOnLoad() in
the Start() function, it will persist across scenes so that it is always available.
Additionally, this class provides some very basic functionality for game level manage-
ment in linearly progressive games; that is, games that progress through the same levels
each time.
The script is designed to use an array of scenes set in the Unity editor Inspector win-
dow. The script retrieves a scene reference from the array as required. It has an integer
variable named gameLevelNum, which is used as an index for the array. To advance to the
next level, gameLevelNum is incremented.
Below are the level management functions:
GoNextLevel()—Increments gameLevelNum and loads the next scene in the
array. If gameLevelNum is higher than the array length, it loops back to zero.
ResetGame()—Resets gameLevelNum back to zero, as though the game were
restarting.
Below is the script in full:
using UnityEngine;
using System.Collections;
public class SceneManager : MonoBehavior
{
public string[] levelNames;
public int gameLevelNum;
public void Start ()
{
// keep this object alive
DontDestroyOnLoad (this.gameObject);
}
public void LoadLevel( string sceneName )
{
Application.LoadLevel( sceneName );
}
public void ResetGame()
{
// reset the level index counter
gameLevelNum = 0;
}
Search WWH ::




Custom Search