纳金网
标题:
业余练习:单例模版
[打印本页]
作者:
烟雨
时间:
2016-5-30 00:49
标题:
业余练习:单例模版
单例模版:
单例我就不说了,直接贴代码就行:
//非MonoBehaviour 的模版 就是不需要在Hierarchey中创建物体的单例
public class SigletonNotMono<T> where T :class,new()
{
private static T _instance;
public static T Instance
{
get{
if(_instance==null)
_instance = new T();
return _instance;
}
}
}
//MonoBehaviour 单例模版,在游戏创建物体
public class SigletonMono<T> : MonoBehaviour where T:MonoBehaviour
{
private static T _instance;
public static T Instance
{
get{
if(_instance==null)
{
GameObject singleton = new GameObject();
_instance = singleton.AddComponent<T>();
singleton.name = "(singleton)"+typeof(T).ToString();
DontDestroyOnLoad(singleton);
}
return _instance;
}
}
}
复制代码
欢迎光临 纳金网 (http://course.narkii.com/club/)
Powered by Discuz! X2.5