Unity singleton pattern base class

using UnityEngine;

public class SingletonBase<T> where T : new()
{
    
    
    private static T instance;
    public static T Instance
    {
    
    
        get
        {
    
    
            if (instance == null)
            {
    
    
                if (instance == null)
                    instance = new T();
            }
            return instance;
        }
    }
}

public class SingletonMonoBase<T> : MonoBehaviour where T : MonoBehaviour
{
    
    
    private static T instance;
    public static T Instance
    {
    
    
        get
        {
    
    
            if (instance == null)
            {
    
    
                GameObject obj = new GameObject();
                //设置对象的名字为脚本名
                obj.name = typeof(T).ToString();     
                //让这个单例模式对象 过场景 不移除
                //因为 单例模式对象 往往 是存在整个程序生命周期中的
                DontDestroyOnLoad(obj);
                instance = obj.AddComponent<T>();
            }
            return instance;
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_45136016/article/details/133135500