Unity singleton mode is relatively simple to understand

This article will introduce the practical use of the singleton pattern in unity from the simplest to the most complex.

The singleton pattern is a design pattern that represents best practices and is usually adopted by experienced object-oriented software developers. Design patterns are solutions to common problems that software developers face during software development. These solutions are the result of trial and error by numerous software developers over a considerable period of time.

First write the simplest singleton pattern. The member variable of this class is an object of its own, and this variable is set to itself when initialized.

Then we give this script to some object:

The instance of the script at this point is itself.

Then we can directly call the class.instance.method in another script to directly call the functions in the class:

In general, there is no need to inherit from MonoBehaviour.

 

The following is a detailed explanation of the singleton pattern:

Singleton pattern refers to a design pattern in which objects are created only once in memory . When the same object is used multiple times in the program and has the same function, in order to prevent the memory from soaring due to the frequent creation of objects, the singleton mode allows the program to create only one object in memory, so that all places that need to be called share this singleton object.

insert image description here

Under normal circumstances, we will define the class constructor as public access, allowing any class to create objects of that class under any circumstances. However, in some cases, it is meaningless to allow other classes to freely create objects of this class, and it may even cause system performance degradation, because frequently creating and recycling objects will bring system overhead.

If only one instance of a class can always be created, the class is called a singleton class.
There are three limitations to use:

  1. In short, in some special scenarios, in order to prevent other classes from freely creating objects of this class, the constructors of this class should be decorated with private, so as to hide all the constructors of this class.
  2. According to the principle of good encapsulation: once the constructor of the class is hidden, a public method needs to be provided as the access entry of the class to create objects of the class, and the method must be decorated with static, because before calling the method Objects don't exist yet, so it can't be objects that call this method, only classes.
  3. In addition to this, the class must also cache the objects that have been created, otherwise the class has no way of knowing whether an object has been created or not, and it cannot guarantee that only one object is created. For this purpose, the class needs to use a member variable to save the object once created, because the member variable needs to be accessed by the above static method, so the member variable must be modified with static.

So the code is as follows:

insert image description here

In Unity, the above method of getting an instance can be changed to use a public variable, and then set its get method. The code is as follows:

(If the constructor is missing, the system will automatically generate it, so it is not necessary to write it here)

Then you can directly call the method in the class name.Instance. class directly on it in other places.

Guess you like

Origin blog.csdn.net/weixin_43757333/article/details/122849326