Unity advanced study notes: singleton mode

Game frame:

Game frameworks generally include message frameworks, state machines, managers, and tools.

The message framework refers to the communication framework between game objects. Although the unity engine comes with a set of message framework, this framework can only be used for communication between parent and child objects, and it cannot realize communication between most non-parent-child relationship objects. Therefore, we will complete a set of message frameworks by ourselves in the follow-up

The state machine is used to manage the different states of each object. For example, a character may have multiple states such as movement, attack, and death. Use state machines for better state management and switching

Managers are used to complete a specific game function. Such as role manager, backpack manager, task system manager, etc.

The tool class is used to encapsulate some functions or functions that will appear repeatedly, such as creating random numbers, generating damage, encrypting content, etc.

Singleton class:

A singleton generates only one object per class. Generally speaking, the management class of game content, such as message management class and network management class, should only have one object, and a singleton should be used to implement it.

Every time a script is mounted on a game object as a component in the game, an object of the script class will be generated. Therefore, to implement a singleton, you must ensure that only one object is mounted

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestMonobehavior : MonoBehaviour
{
    
    

    private static TestMonobehavior instance;
    public static TestMonobehavior Instance {
    
    
        get {
    
    
            return instance;
        }
    }

    
    void Awake()
    {
    
    
        instance = this;
    }

    // Update is called once per frame
    void Update()
    {
    
    
        
    }
}

In this instance, we create a singleton object instance (the common name of a general singleton object) for access to the outside world. And define a static method to return the singleton object. In this way, the outside world can access the singleton through TestMonobehavior.Instance

We consider that there may be multiple singleton objects (such as multiple management classes) in a game. So we can write a generic singleton class base class, and then let subsequent singleton classes inherit the base class

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SingletonBase<T> : MonoBehaviour where T : MonoBehavior
{
    
    

    private static T instance;
    public static T Instance {
    
    
        get {
    
    
            return instance
        }
    }

    protected virtual void Awake()
    {
    
    
       instance = this as T; 
    }

    
    protected virtual void OnDestroy()
    {
    
    
        instance = null;
    }
}

Here our program is basically the same as above, except that the instance is set as a generic object. Also note the following points:

public class SingletonBase<T> : MonoBehaviour where T : MonoBehavior

In the class definition, where T : MonoBehavior means that the generic type T must be a subclass of MonoBehavior (unity game object class)

protected virtual void Awake()

The Awake() and Destroy() methods here may be overridden in subclasses. So we set it as an abstract method of protected permissions (not externally accessible, but inheritable)

For normal classes (non-unity scripts), singletons can be implemented using the following method

public class Test : MonoBehaviour
{
    
    

    private static Test instance;
    public static Test Instance {
    
    
        get {
    
    
        	if (instance == null) {
    
    
        		instance = new Test();
        	}
            return instance;
        }
}

Here we first judge whether the instance is created in the Instance method, which is used to create a singleton when the singleton method is called for the first time. After that, we use return instance normally to get the singleton object

Guess you like

Origin blog.csdn.net/Raine_Yang/article/details/130498794