Unity framework learning--3

Singleton pattern base class

Constructor privatization to prevent external creation of objects

Provide an attribute for external access. This attribute is equivalent to the only object of this class.

Divided into lazy man mode and hungry man mode

Singleton mode that does not inherit MonoBehaviour

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

Inherit the singleton mode of MonoBehaviour

public class MyUiManager : MonoBehaviour
{
    private MyUiManager() { }

    private static MyUiManager instance;

    public static MyUiManager Instance 
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<MyUiManager>();
            }
            return instance;
        }
    }
}

 instance = FindObjectOfType<MyUiManager>();

 The singleton that inherits MonoBehaviour will be hung on the game scene. You need to find it in the game scene and assign it.

Inheritance and non-inheritance have different initialization methods.

Inherit the automatic singleton mode of MonoBehaviour

if (instance == null)
            {
                instance = FindObjectOfType<MyUIManager>();
                //游戏场景中没有创建物体挂在脚本,写代码来自动做这些事情
                if (instance == null)
                {
                    GameObject go = new GameObject("MyUIManager");  //创建游戏对象
                    instance = go.AddComponent<MyUIManager>();  //挂载脚本到游戏对象身上
                }
            }

 The problem of switching scenes in singleton mode inheriting MonoBehaviour

if (instance == null)
                {
                    GameObject go = new GameObject("MyUIManager");  //创建游戏对象
                    instance = go.AddComponent<MyUIManager>();  //挂载脚本到游戏对象身上
                }
                DontDestroyOnLoad(instance);   //让游戏对象切换场景时不销毁

Singleton mode base class that does not inherit MonoBehaviour

/// <summary>
/// 不继承MonoBehaviour 的单例模式基类
/// 作用:继承了这个这个类的类自带单例模式
/// </summary>
public class SingletonPatternBase<T> where T: SingletonPatternBase<T>
{
    private static T instance;

    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                //where 里面要求含有无参构造函数  where T :new()
                //....单例怎么能有无参构造函数呢
                //instance = new T();
                
                //或者利用反射调用无参构造方法来构造
                instance = Activator.CreateInstance(typeof(T), true) as T;
            }
            return instance;
        }
    }

    //构造方法私有化  private 的话子类也无法继承对象,所以使用protected
    protected SingletonPatternBase() { }

}

Singleton mode base class that inherits MonoBehaviour

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

public class SingletonMonoAutoBase<T> : MonoBehaviour where T:MonoBehaviour
{
    protected SingletonMonoAutoBase() { }

    private static T instance;

    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<T>();
                if (instance == null)
                {
                    GameObject go = new GameObject(typeof(T).Name);  //创建游戏对象
                    instance = go.AddComponent<T>();  //挂载脚本到游戏对象身上
                }
            }
            return instance;
        }
    }
}

Issues with scene switching in the singleton mode base class that inherits MonoBehaviour

Problem accessing singleton object in OnDestroy method

When the OnDestroy method is executed, the resources will be cleared, the instance detection is empty, and a new object will be automatically generated, so an error will be reported.

Solution:

    //Record whether the singleton object exists, used to prevent errors when accessing the singleton object in the OnDestory method
    public static bool isExisted { get; private set; } = false;

protected virtual void OnDestroy()
    {   
        isExisted = false; 
    } 

 Problems encountered when multi-threads access singletons

 Operating system thread lock problem

//线程锁。当多线程访问时,同一时刻只允许一个线程访问
    private static object locker = new object();

    //volatile关键字修饰的字段,当多个线程都对它进行修改时,可以确保这个字段在任何时刻呈现的都是最新的值
    private volatile static T instance;

    public  static T Instance
    {
        get
        {
            if (instance == null)
            {
                lock(locker)
                {
                    if (instance == null)
                    {
                        //或者利用反射调用无参构造方法来构造
                        instance = Activator.CreateInstance(typeof(T), true) as T;
                    }
                }
            }
            
            return instance;
        }
    }

Guess you like

Origin blog.csdn.net/zaizai1007/article/details/132279554