- Unity Certified Programmer:Exam Guide
- Philip Walker
- 689字
- 2021-06-18 18:30:11
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:
- 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.
- Next, we need to check and assign our instance variable with the GameManager class when the script begins with the Awake function.
- 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.
- 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);
}
- 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.
- Call the method from the Awake function.
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:

- 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).
- Functional Python Programming
- C程序設(shè)計簡明教程(第二版)
- Leap Motion Development Essentials
- Rake Task Management Essentials
- Python機器學習實戰(zhàn)
- QGIS:Becoming a GIS Power User
- Responsive Web Design by Example
- Linux命令行與shell腳本編程大全(第4版)
- Visual FoxPro程序設(shè)計習題集及實驗指導(dǎo)(第四版)
- 低代碼平臺開發(fā)實踐:基于React
- Python深度學習原理、算法與案例
- Java零基礎(chǔ)實戰(zhàn)
- Xcode 6 Essentials
- 深入解析Java編譯器:源碼剖析與實例詳解
- Java Web動態(tài)網(wǎng)站開發(fā)(第2版·微課版)