官术网_书友最值得收藏!

Adding a Singleton design pattern

As you will recall, back in Chapter 1, Setting Up and Structuring Our Project, we spoke about design patterns and how useful they are to help maintain our code. One of the design patterns we briefly covered was the Singleton pattern. Without repeating ourselves, the Singleton pattern gives us global access to code that can then be obtained nearly at a point in our game. So, where can we see the benefits of using the Singleton design pattern? Well, we could use it so that Unity always keeps certain scripts accessible, no matter what scene we are in. We have already added a lot of structuring to our game framework and we still have a couple of manager scripts to add, such as ScoreManager and ScenesManager.

Now is a good time to make it so that all of the manager scripts have global access to all other scripts in the game. Managers give a general overview of what is going on and steer which way the game needs to go without getting caught up in the details of the other scripts that are running during gameplay.

In our current setup, when we run the testLevel scene, our GameManager object is in the Hierarchy window. We also have—and will be adding—more manager scripts to this game object. Currently, when we change scenes, our GameManager script, which sets up our scene's camera and lights, is no longer present.

To stop our GameManager game object and script from being wiped, we are going to add a Singleton design pattern so that our GameManager script will always be in the scene. This design pattern will also make it so that there is only one GameManager script (which is where this design pattern gets its name from).

In the following instructions, we will extend our original GameManager code to work as a Singleton script. Double-click on the GameManager script and let's make a start:

  1. At the beginning of the class, we need to add a static variable and a public static property, both referring to our GameManager script:
static GameManager instance;
public static GameManager Instance
{
get { return instance; }
}

The reason we do this is that static means there is only one type of game manager. This is what we want; we don't want to have multiple versions of the same manager.

  1. Next, we need to check and assign our instance variable with the GameManager class when the script begins with the Awake function.
  2. The Awake function ends with a Unity function called DontDestroyOnLoad. This will make sure the game object holding our GameManager class will not be destroyed if the scene changes.
If the player dies and loses all their lives, we can move from the level scene we are on to the gameOver scene, but we won't wipe the GameManager game object out from the scene as this holds the main core methods to run the game.
  1. Add an else loop to avoid any possible duplicate GameManager game objects. We can see these two steps in the following code block:
  void Awake()
{
if(instance == null)
{
instance = this;
}
else
{
Destroy(this.gameObject);
}
DontDestroyOnLoad(this);
}
  1. To make our code easier to identify, wrap the code we just typed out in the Awake function and put it in a method calledCheckGameManagerIsInTheScene.
  2. Call the method from the Awake function.
A similar method to DontDestroyOnLoad is MoveGameObjectToScene, which can be used to carry a single game object over to another scene. This could be useful for moving a player from one scene to another:

https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.MoveGameObjectToScene.html

That's it, our Singleton design pattern is done! The following screenshot shows a snippet of what our GameManager script should look like:

  1. Finally, save the GameManager script.

We have created a Singleton design pattern that will not be wiped away when we alternate through the scenes in our game, giving us global control of our game no matter which scene we are in.

Now, we can jump into adding the ScenesManager script and attaching it to the same game object as GameManager (in its Inspector window).

主站蜘蛛池模板: 宜君县| 和平区| 于田县| 贵德县| 吉隆县| 鹤庆县| 乌拉特后旗| 乡宁县| 龙山县| 长武县| 象山县| 古田县| 色达县| 鄂托克旗| 田阳县| 射阳县| 兴安县| 泸水县| 高尔夫| 抚州市| 张家界市| 兴海县| 盐津县| 台湾省| 赤城县| 抚顺县| 德庆县| 仁化县| 龙口市| 寿宁县| 龙井市| 赣州市| 梨树县| 鸡西市| 疏勒县| 梧州市| 嘉善县| 成安县| 大新县| 静安区| 永宁县|