UIFramework之通用单例类Singleton

UIFramework之通用单例类Singleton

在Unity的开发中,单例模式是最经常使用的设计模式之一,在每个模块开发中,如果都单独去实现单例模式,会很繁琐,所以实现一个Singleton类,每个需要实现单例模式的模块只需要集成自该类,即可。

[csharp]  view plain  copy
 print ?
  1. /* 
  2. * Name: SingletonManager.cs 
  3. * Function: N/A  
  4.  
  5. * Ver     变更日期               负责人                            变更内容 
  6. * ────────────────────────────────────────────────────────────────────── 
  7. * V1.0.0  $time$    http://blog.csdn.net/husheng0      
  8.  
  9. * Copyright (c). All rights reserved. 
  10. * 
  11. * 单例类 
  12.  
  13. */  
  14.   
  15. using UnityEngine;  
  16. using System.Collections;  
  17. /// <summary>  
  18. /// 单例管理类的实现,不继承Monobehaviour  
  19. /// </summary>  
  20. /// <typeparam name="T"></typeparam>  
  21. public class SingletonManager<T> where T : class,new()  
  22. {  
  23.   
  24.     protected static T instance = null;  
  25.   
  26.     public static T Instance  
  27.     {  
  28.         get  
  29.         {  
  30.             if (null == instance)  
  31.             {  
  32.                 instance = new T(); //调用构造函数    
  33.             }  
  34.             return instance;  
  35.         }  
  36.     }  
  37.     /// <summary>  
  38.     /// SingletonManager构造函数  
  39.     /// </summary>  
  40.     protected SingletonManager()  
  41.     {  
  42.         if (null != instance)  
  43.             Debug.Log("This " + (typeof(T)).ToString()   
  44. " Singleton Instance is not null!");  
  45.         Init();  
  46.     }  
  47.     public virtual void Init() { }  
  48. }  
[csharp]  view plain  copy
 print ?
  1. /* 
  2. * Name: MonoSingletonManager.cs 
  3. * Function: N/A  
  4.  
  5. * Ver     变更日期               负责人                            变更内容 
  6. * ────────────────────────────────────────────────────────────────────── 
  7. * V1.0.0  $time$    http://blog.csdn.net/husheng0      
  8.  
  9. * Copyright (c). All rights reserved. 
  10. * 
  11. * 单例类  
  12.  
  13. */  
  14.   
  15. using UnityEngine;  
  16. using System.Collections;  
  17. /// <summary>  
  18. /// 单例管理类的实现,继承Monobehaviour  
  19. /// 该类会在Hierarchy下创建"___MonoSingleton"并把所有的集成MonoSingletonManager的脚本添加到"___MonoSingleton"上  
  20. /// </summary>  
  21. /// <typeparam name="T"></typeparam>  
  22. public class MonoSingletonManager<T> : MonoBehaviour where T : MonoSingletonManager<T>  
  23. {  
  24.   
  25.     private static T instance = null;  
  26.   
  27.     public static T Instance  
  28.     {  
  29.         get  
  30.         {  
  31.             if (null == instance)  
  32.             {  
  33.                 GameObject go = GameObject.Find("___MonoSingleton");  
  34.                 if (null == go)  
  35.                 {  
  36.                     go = new GameObject("___MonoSingleton");  
  37.                     DontDestroyOnLoad(go);  
  38.                 }  
  39.                 instance = go.AddComponent<T>();  
  40.   
  41.             }  
  42.             return instance;  
  43.         }  
  44.     }  
  45.   
  46.     /// <summary>  
  47.     /// Raises the application quit event.  
  48.     /// </summary>  
  49.     private void OnApplicationQuit()  
  50.     {  
  51.         instance = null;  
  52.     }  
  53. }  

=====================================================================

结束。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值