Chinaunix首页 | 论坛 | 博客
  • 博客访问: 41885
  • 博文数量: 22
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 344
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-19 11:34
文章分类
文章存档

2014年(22)

我的朋友

分类: Android平台

2014-04-23 17:29:44


中的单例模式通常用法
通常用法是在相关类加入GetInstance()的静态方法,检查实例是否存在。如果存在,则返回。如果不存在,则返回一个“需要用游戏元素类关联”的调试警告错误。
public class MyClass   
{  
private static MyClass instance;   
public static MyClass GetInstance()  
{  
if (!instance)  
{  
instance = GameObject.FindObjectOfType(typeof(MyClass));   
if (!instance)  
Debug.LogError("There needs to be one active MyClass script on a GameObject in your scene.");   
}  
return instance;   
}
}
 


阅读(1288) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~