//1.C#中 泛型的知识
//2.设计模式种 单例模式的知识
public class BaseManager<T> where T : new()
{
private static T instance;
public static T GetInstance()
{
if(instance == null)
instance = new T();
return instance;
}
}
多线程状态下要加上双锁
继承MonoBehavior的单例模式基类
不可取的方式
public class NewBehaviourScript : MonoBehaviour
{
private static NewBehaviourScript instance;
public static NewBehaviourScript GetInstance()
{
//继承Mono的脚本 不能够直接new
//只能通过拖动到对象上 或者 通过加脚本的api AddComponent去加脚本
//U3D内部帮助我们实例化它
//但挂载到多个对象上,最后instance会是最后挂载的对象
//这破坏了单例模式
return instance;
}
private void Awake()
{
instance = this;
}
}
可取的方式
//1.C#中 泛型的知识
//2.设计模式种 单例模式的知识
//继承MonoBehavior 的单例模式对象 需要我们自己保证它的位移性
public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
{
private static T instance;
public static T GetInstance()
{
return instance;
}
//保护虚函数 防止子类顶掉父类的Awake
protected virtual void Awake()
{
instance = this as T;
}
//这样子类要重写并保留base.Awake();
/*protected override void Awake()
{
base.Awake();
}*/
}
更好的方式
//继承这种自动创建的 单例模式基类 不需要我们手动去拖 或者 api去加
//想用它 直接 GetInstance
public class SingletonAutoMono<T> : MonoBehaviour where T : MonoBehaviour
{
private static T instance;
public static T GetInstance()
{
if (instance == null)
{
GameObject obj = new GameObject();
//设置对象的名字为脚本名
obj.name = typeof(T).ToString();
instance = obj.AddComponent<T>();
}
return instance;
}
}
但仍然有缺点:切场景时挂载脚本的物体没了,因此要改进
改进后
public class SingletonAutoMono<T> : MonoBehaviour where T : MonoBehaviour
{
private static T instance;
public static T GetInstance()
{
if (instance == null)
{
GameObject obj = new GameObject();
//设置对象的名字为脚本名
obj.name = typeof(T).ToString();
//让这个单例模式对象 过场景 不移除
//因为 单例模式对象 往往 是存在整个生命周期中
DontDestroyOnLoad(obj);
instance = obj.AddComponent<T>();
}
return instance;
}
}