1. 构造函数带来的唯一性问题指什么?
- 对于不继承MonoBehaviour的单例模式基类
我们要避免在外部 new 单例模式类对象 
例如 (完整单例模式定义在上一节)
            
            
              csharp
              
              
            
          
          public class Main : MonoBehaviour
{
    void Start()
    {
        // 破坏单例模式的唯一性,可以自己new对应的单例模式类对象,这样不安全
        TestMgr t = new TestMgr();
        BaseManager<TestMgr> baseManager = new BaseManager<TestMgr>();
        
    }
}
        - 对于继承MonoBehaviour的单例模式基类
由于继承MonoBehaviour的脚本不能通过new创建,因此不用过多考虑 
2. 解决构造函数带来的安全问题
解决BaseManager baseManager = new BaseManager();问题
- 父类变成抽象类


 
解决 TestMgr t = new TestMgr();问题
- 规定继承单例模式基类的类必须显示实现私有无参构造函数

 - 但是会出现单例模式基类不能通过new问题创建实例


 - 解决办法:在基类中通过反射来调用私有构造函数实例化对象

 
            
            
              csharp
              
              
            
          
          public abstract class BaseManager<T> where T : class/*,new()*/
{
    public static T instance;
    // 属性的方式
    public static T Instance
    {
        get
        {
            if(instance == null)
            {
                //instance = new T();
                // 利用反射得到无参私有的构造函数,来用于对象的实例化
                Type type = typeof(T);
                //  BindingFlags.Instance | BindingFlags.NonPublic, //表示成员私有方法
                //    null,                                         //表示没有绑定对象
                //    Type.EmptyTypes,                              //表示没有参数
                //    null);                                        //表示没有参数修饰符
                ConstructorInfo info =  type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null,Type.EmptyTypes,null);
                if(info != null)
                {
                    instance = info.Invoke(null) as T;
                }
                else
                {
                    Debug.LogError("没有得到对应的无参构造函数");
                }
                
            }
            return instance;
        }
    }
    
}
        - 这样就实现了外部不能new单例模式的对象,同时,单例模式的基类通过反射获取到构造函数创建实例
