Unity 单例模式基类模块

//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;
        }
    
}

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值