Unity中的泛型单例脚本工具

在Unity游戏开发中,写脚本经常会用到单例模式,所以就写了一个公用的泛型单例类方便使用,也为了以后开发偷点懒

 

public class Singleton<T> where T : class, new()
{
    public Singleton()
    {

    }

    private static T _instance;

    private static readonly object _synclock = new object();


    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (_synclock)
                {
                    if (_instance == null)
                    {
                        _instance = new T();
                    }
                }
            }
            return _instance;
        }
        set { _instance = value; }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章