- Unity Certified Programmer:Exam Guide
- Philip Walker
- 1881字
- 2021-06-18 18:30:11
Setting up our ScenesManager script
We will take some responsibility away from the GameManager script by making another manager script to be more consistent with the data and methods it holds. ScenesManager will take and send information to and from GameManager. The following diagram shows how close to GameManager our ScenesManager script exists within the framework when only communicating with GameManager:

The purpose of ScenesManager, apart from taking the workload off GameManager, is to hold the role of dealing with anything related to creating or changing a scene. This doesn't mean we focus on only adding and removing game levels; a scene can also consist of a start-up logo, a title screen, a menu, and a game over screen, all of which are part of the ScenesManager script's responsibility.
In this section, we will be setting up a scene template and two methods. The first method will be responsible for resetting the level if the player dies (ResetScene()); the second will be the game over screen (GameOver()).
Let's make a start by creating a new script in the same way that we did in Chapter 2, Adding and Manipulating Objects. Follow these steps:
- Name the script ScenesManager.
- Add the script to the GameManager game object. If you need further details on adding a script to a game object, check out the Adding our script to a game object section of the previous chapter.
- With our GameManager game object selected from the Hierarchy window, go to the Inspector window. We should now have the GameManager and ScenesManager scripts attached, as in the following screenshot:

Let's open the ScenesManager script and start coding:
- Because we are obviously going to be closing and loading scenes, we are going to need to import an extra library into our ScenesManager script that supports these operations:
using UnityEngine.SceneManagement;
using UnityEngine;
- We will have a public class in our script name, followed by the usual MonoBehaviour being inherited:
public class ScenesManager : MonoBehaviour
{
Now, we need to create a list of references for our scenes, as mentioned earlier. I currently have the following scenes labeled:
-
- bootUp: Credits to game
- title: Name of the game with an instruction to start
- shop: Buy upgrades before starting the game
- level1: First level
- level2: Second level
- level3: Final level
- gameOver: Game over—delays until going back to the title scene
We will be labeling these scenes as enumerations (which is recognized as enum in the C# language). These values stay consistent.
- Enter the following code into the ScenesManager script:
Scenes scenes;
public enum Scenes
{
bootUp,
title,
shop,
level1,
level2,
level3,
gameOver
}
We will be making and adding these scenes in their respective order in the Unity editor. Before we do so, let's add two methods, starting with the ResetScene() method, which is typically used when the player dies and the current level is reloaded. The other method, GameOver(), istypicallycalled when the player loses all of their lives or when the game has completed.
Adding the ResetScene() method
The ResetScene() method will be called when the player loses a life but still has another remaining. In this short method, we will set its accessibility to public and it returns nothing (void).
Within this method, we will refer to Unity's SceneManager script (not to be confused with our ScenesManagerclass), followed by Unity'sLoadScenemethod. We now need to provide a parameter to tellLoadScenewhich scene we are going to load.
We use Unity's SceneManager script again, but this time we use GetActiveScene().buildIndex, which basically means getting the value number of the scene. We send this scene number to SceneManager to load the scene again (LoadScene):
public void ResetScene()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
A small but effective method, this can be called whenever we need the scene to reset. Let's now move on to the GameOver() method.
Adding the GameOver() method
This method, as you can expect, is called when the player has lost all of their lives and the game ends, which means we need to move the player on to another scene.
In this method, we continue adding to the ScenesManager script:
public void GameOver()
{
SceneManager.LoadScene("gameOver");
}
}
Similar to the previous method, we refer to this method as public with void return. Within the method, we call the same Unity function, SceneManager.LoadScene, but this time, we call the SceneManagerUnity function,followed by the name of the scene we want to load by name (in this case,gameOver).
That's our GameOver() method made, and when used in the same way as our ResetScene() method, it can be called globally. GameOver() can be called not onlywhen the player loses all their lives but also when the user completes the game. It can also be used if, somehow, the game crashes, and as a default reset, we proceed to thegameOverscene.
The next method to bring into our ScenesManager script is BeginGame(). This method is called when we need to start playing our game.
Adding the BeginGame() method
In this short section, we will add the BeginGame() method to our ScenesManager script as this will be called to start playing our game after visiting the shop scene, which we will cover in Chapter 5,Creating a Shop Scene for Our Game.
With the ScenesManager script still open from the previous section, add the following method:
public void BeginGame()
{
SceneManager.LoadScene("testLevel");
}
The preceding code that we have just entered makes a direct call to run the testLevel scene, which we play our game in already. However, as our game begins to grow, we will use more than one scene.
The next thing to do is to create our scenes and add them to the Unity build menu, so let's do that next. Remember to save the ScenesManager script before returning back to the Unity editor.
Adding scenes to our Build Settings window
Our game will consist of multiple scenes through which the player will need to navigate before they can fly their spaceship through the levels. This will result in them either dying or completing each level and the game, then being taken back to the title scene. This is also known as a game loop. Let's start by going back to Unity and in the Project window, creating and adding our new scenes. Follow theses steps:
- Go to the Assets/Scene folder that we created at the beginning of the previous chapter.
- Inside the Scene folder, in the open space, right-click so that the dropdown appears, then click Create, followed by Scene, as in the following screenshot:

- A scene file will appear. Rename it bootUp.
- Repeat this process for the shop, level1, level2, level3, gameOver, and title scene files.
Once we have made all of our scenes, we need to let Unity know that we want these scenes to be recognized and applied to the project build order. This is a similar process to what we did in the last chapter when adding testLevel to the Build Settings window. To apply the other scenes to the list, do the following:
- From the top of the Unity editor, click on File | Build Settings.
- The Build Settings window will open and you should have testLevel in the list already. If you don't, fear not as we will be adding all our scenes to the Scenes In Build list.
- From the Project window, click and drag each scene into the Build Settings | ScenesinBuild open space.
Once we have added all the scenes, order them as follows:
- bootUp
- title
- shop
- testLevel
- level1
- level2
- level3
- gameOver
The Build Settings window should now look as follows:

The reason why we are putting our scenes in this order is so that there is a logical progression in the levels. As you can see at the far right of each scene in the previous screenshot, the scenes are counted in increments.
Now that we have added multiple scenes to our game, we can consider the fact that we may not want our camera and light setup methods in our GameManager method to run in every scene of our game. Let's briefly return to our GameManager script and update our LightSetup and CameraSetup methods, as well as a few other things.
Updating our GameManager script
In this section, we are going to return to the GameManager script and make it so that the CameraSetup and LightSetup methods are called when we are controlling our spaceship only.
To update our GameManager script to support various scenes for our lights and camera, we need to do the following:
- In the Unity editor, navigate to Assets/Resources/Script/GameManager from the Project window.
- In the GameManager script, scroll down to the Start function and remove the LightSetup(); and CameraSetup();methods.
- Next, we will enter two static global variables at the top of the GameManager script with the rest of the global variables:
public static int currentScene = 0;
public static int gameLevelScene = 3;
bool died = false;
public bool Died
{
get {return died;}
set {died = value;}
}
currentScene is an integer that will keep the number of the current scene we are on, which we will use in the following method. The second variable, gameLevelScene, will hold the first level we play, which we will use later on in this chapter.
- Still in the GameManager script, create an Awake function and enter the following code:
void Awake()
{
CheckGameManagerIsInTheScene();
currentScene = UnityEngine.SceneManagement.SceneManager.
GetActiveScene().buildIndex;
LightandCameraSetup(currentScene);
}
In the code we just entered, we store the buildIndex number (the numbers we have to the right of each scene in our Build Settings window from the previous section) in the currentScene variable. We then send the currentScene value to our new LightandCameraSetup method.
- The last piece of code to add to our GameManager script is the LightandCameraSetup method, which takes an integer parameter:
void LightandCameraSetup(int sceneNumber)
{
switch (sceneNumber)
{
//testLevel, Level1, Level2, Level3
case 3 : case 4 :case 5: case 6:
{
LightSetup();
CameraSetup();
break;
}
}
}
In the code we just wrote, we ran a switch statement to check the value of the sceneNumber variable, and if it falls into the 3, 4, 5, or 6 values, we run LightSetup and CameraSetup.
- Save the GameManager script.
To reflect on this section, we have created a structure of empty scenes that will each serve a purpose in our game. We have also created a ScenesManager script that will either reset a scene when the player wins or dies and/or move to the game over scene.
Now that we have our scenes in place and the start of the ScenesManager script has been built, we can focus on the player's life system.
- Google Apps Script for Beginners
- .NET之美:.NET關(guān)鍵技術(shù)深入解析
- GeoServer Cookbook
- JIRA 7 Administration Cookbook(Second Edition)
- AngularJS Web Application Development Blueprints
- Visual C++數(shù)字圖像模式識別技術(shù)詳解
- 數(shù)據(jù)結(jié)構(gòu)與算法JavaScript描述
- Unity Game Development Scripting
- Hands-On Natural Language Processing with Python
- Express Web Application Development
- Mobile Device Exploitation Cookbook
- Extreme C
- Quantum Computing and Blockchain in Business
- 持續(xù)集成與持續(xù)交付實戰(zhàn):用Jenkins、Travis CI和CircleCI構(gòu)建和發(fā)布大規(guī)模高質(zhì)量軟件
- Building Serverless Web Applications