///
/// 万能单利模式:哪个类需要变成单利模式,只需要继承该类就可以
/// 继承该类的脚本可以不挂在游戏对象上,也可以挂在游戏对象上
/// 如Person类为单利模式:public class Person :MonoSingle{}
/// 这样写就可以了
///
///
public class MonoSingle : MonoBehaviour where T:MonoSingle{
private static T instance;
public static T Instance
{
get
{
if(instance==null)
{
GameObject go = GameObject.Find(typeof(T).Name);
if(go==null)
{
go = new GameObject();
instance = go.AddComponent();
}else
{
if(go.GetComponent()==null)
{
go.AddComponent();
}
instance = go.GetComponent();
}
go.name = typeof(T).Name;
DontDestroyOnLoad(go);
}
return instance;
}
}
}
万能单利模式的使用
最新推荐文章于 2023-02-13 19:26:28 发布