unity3d研究之c# 单例模式

在Unity3D中可以用不同的方法来实现单例,第一种是通常用法,第二种是自我包含法,第三种方法粗制滥造法,第四种是为C#开发者准备的,计数器法。
1.通常用法
     通常用法是在相关类加入GetInstance()的静态方法,检查实例是否存在。如果存在,则返回。如果不存在,则返回一个“需要用游戏元素类关联”的调试警告错误.
  1. 01 public class MyClass  

  2. 02 {  

  3. 03     private static MyClass instance;  

  4. 04     public static MyClass GetInstance()  

  5. 05     {  

  6. 06         if (!instance)  

  7. 07         {  

  8. 08             instance = GameObject.FindObjectOfType(typeof(MyClass));  

  9. 09             if (!instance)  

  10. 10                 Debug.LogError("There needs to be one active MyClass script on a GameObject in your scene.");  

  11. 11         }  

  12. 12    

  13. 13         return instance;  

  14. 14     }  

  15. 15 }
复制代码
2.自我包含法
     有一次玩Trench Run game,我意识到我的场景类里存在许多的GameObject。所以,我开发了自我包含的单例。如果没找找到实例,就会创建它自己的GameObject,注重通过AddComponent()方法返回来关联实例类,而不需要在IDE中创建一个GameObject在设计时弄乱你的场景。
  1. 01 public class Logger : MonoBehaviour  

  2. 02 {  

  3. 03     private static Logger instance;  

  4. 04     private static GameObject container;  

  5. 05     public static Logger GetInstance()  

  6. 06     {  

  7. 07         if( !instance )  

  8. 08         {  

  9. 09             container = new GameObject();  

  10. 10             container.name = "Logger";  

  11. 11             instance = container.AddComponent(typeof(Logger)) as Logger;  

  12. 12         }  

  13. 13         return instance;  

  14. 14     }  

  15. 15 }
复制代码
3.粗制滥造法
    粗制滥造法很简单,为实例设置一个公共静态属性,初始化Awake()方法,设计时关联一个GameObject. 可以用以下方法访问:
  1. MyClass.instance.DoSomething();
  2.    1 public class MyClass  

  3. 2 {  

  4. 3     public static MyClass instance;  

  5. 4     public void Awake()  

  6. 5     {  

  7. 6         MyClass.instance = this;  

  8. 7     }  

  9. 8 }
复制代码

在ActionScript里访问外在类的其他方法比访问一个属性要慢很多,我不知道这是否属实(我怀疑),但是在过去几年里我在Flash上噩梦般的优化,我通常用的是这种方法。也许有些习惯永远也改不了的!(或者我本来就偏执于此)
   祝你愉快!
4.计数器法
    以上的第一种方法和第二种方法得利于使用访问器而不是一个方法。关于本提示要感谢Cliff Owen.
  1. 01 public class MyClass  

  2. 02 {  

  3. 03     private static MyClass _instance;  

  4. 04     public static MyClass Instance  

  5. 05     {  

  6. 06         get 

  7. 07         {  

  8. 08             if (!_instance)  

  9. 09             {  

  10. 10                 _instance = GameObject.FindObjectOfType(typeof(MyClass));  

  11. 11                 if (!_instance)  

  12. 12                 {  

  13. 13                     GameObject container = new GameObject();  

  14. 14                     container.name = "MyClassContainer";  

  15. 15                     _instance = container.AddComponent(typeof(MyClass)) as MyClass;  

  16. 16                 }  

  17. 17             }  

  18. 18    

  19. 19             return _instance;  

  20. 20         }  

  21. 21     }  

  22. 22 }
复制代码

然后你就可以用以下方士简单而粗制滥造的访问它:

MyClass.Instance.DoSomething();

原文:

http://bbs.9ria.com/thread-66692-1-1.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值