01 单例模式

using UnityEngine;

public class Singleton : MonoBehaviour
{
    ////饿汉式单例
    ////private static Singleton _instance = new();
    //private static Singleton _instance ;
    //public static Singleton Instance => _instance;
    //private void Awake()=>_instance = this;

    //懒汉式单例
    private static Singleton _instance;
    public static Singleton Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new Singleton();
            }
            return _instance;
        }
    }

}

//单例模板
public abstract class SingletonTemplate<T>:MonoBehaviour
    where T : MonoBehaviour
{
    private static T _instance;
    public static T Instance => _instance;

    private void Awake()=>_instance=this as T;
   
}

 

posted on 2024-02-25 22:55  莫凡-荒天帝  阅读(1)  评论(0编辑  收藏  举报

导航