两个单例模板

单例在Unity是老生常谈的一个设计模式,并不是它有多么伟大,而是它有多么的方便,当然方便不等于滥用,我们要区分单例用的场合。在我的设计中,大多用来作为管理类,以下两个模板皆来自前辈们的手笔,部分为自己修改。

1.普通类的单例模板

public ClassSigleton<T> where T :Class
{
        private static T _instance;
        /// <summary>
        /// 添加一个锁,防止多线程造成的多个实例,并加入私有构造函数
        /// </summary>
        private static readonly object syncRoot = new object();

        public static readonly Type[] EmptyTypes = new Type[0];
         public static T instance
        { 
           get
           {
               if (_instance == null)
               {
                   lock (syncRoot)
                   {
                       if (_instance == null)
                       {
                           ConstructorInfo ci = typeof(T).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, EmptyTypes, null);
                           if (ci == null)
                           {
                               throw new InvalidOperationException("class must contain a private constructor");
                           }
                           _instance = (T)ci.Invoke(null);
                       }
                   }
               }
               return _instance;
           }
        }
}

2.基于MonoBehaviour

  /// <summary>
 /// MonoBehaviour单利模板类
 /// 一定不要在Destroy函数中直接访问单例模式,非要使用实现判断instance是否存在
    /// </summary>
 public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
 {
       private static T _instance;
       public Transform cacheTransform
       {
           get;
           protected set;
       }

       public GameObject cacheGameObject
       {
           get;
           protected set;
       }
       /// <summary>
       /// 添加一个锁,防止多线程造成的多个实例,并加入私有构造函数
       /// </summary>
       private static readonly object syncRoot = new object();
       public static T instance
       {
           get
           {
               if (_instance == null)
               {
                   lock (syncRoot)
                   {
                       if (_instance == null)
                       {
                           _instance = GameObject.FindObjectOfType(typeof(T)) as T;
                           if (_instance == null)
                           {
                               GameObject coreGameObject = new GameObject(typeof(T).Name);
                               _instance = coreGameObject.AddComponent<T>();
                               DontDestroyOnLoad(coreGameObject);
                           }
                       }
                   }
               }
               return _instance;
           }
       }

       protected virtual void Awake()
       {
           cacheTransform = transform;
           cacheGameObject = gameObject;
       }

       protected virtual void Destroy()
       {
           _instance = null;
       }
 }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 单例模式(SingletonPattern)一般被认为是最简单、最易理解的设计模式,也因为它的简洁易懂,是项目中最...
    成热了阅读 4,292评论 4 34
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,242评论 25 708
  • 原文地址:http://gad.qq.com/program/translateview/7167991 英文版原...
    重装机霸阅读 3,945评论 1 84
  • 门畔脚步响起,勾起我往昔回忆,记忆中期待着你,在门口面带笑意。 09年 初春 爱情来的太快,年少的世界里除了爱,再...
    Cherry樱桃阅读 380评论 0 0
  • 孩子总会各种淘气。今天熊孩子粟用口水涂满脸,让她住手似没听见。我说你再这样我可要打你屁,她看我一眼却毫不在意。我打...
    hattori阅读 465评论 0 0