4.单例模式问题2:重复挂载

一、单例模式重复挂载问题

1. 重复挂载会破坏单例唯一性

MonoBehaviour 单例依附在 GameObject 上,因此可能出现多个相同单例组件。

二、禁止同一 GameObject 重复挂载

可以给单例基类添加:DisallowMultipleComponent(治标不治本)

例如:

复制代码
[DisallowMultipleComponent]
public class SingletonMono<T> : MonoBehaviour where T : SingletonMono<T>
{
}

不能解决不同 GameObject 之间的重复问题

三、运行时检测重复单例

真正保证唯一性的逻辑应该放在 Awake() 中。

复制代码
protected virtual void Awake()
{
    if (instance != null && instance != this)
    {
        Destroy(gameObject);
        return;
    }
    instance = this as T;
    DontDestroyOnLoad(gameObject);
}

四、为什么判断 instance != this

已经存在实例,并且这个实例不是当前对象时,才属于重复对象。

五、Destroy(this) 与 Destroy(gameObject)

Destroy(this)它只销毁当前单例组件,GameObject 本身仍然存在。

Destroy(gameObject);直接删除整个重复对象:

复制代码
if (instance != null && instance != this)
{
    Destroy(gameObject);
    return;
}

六、完整的 MonoBehaviour 单例基类

可以将重复挂载处理直接封装进基类:

复制代码
using UnityEngine;

[DisallowMultipleComponent]
public class SingletonMono<T> : MonoBehaviour where T : SingletonMono<T>
{
    private static T instance;
    public static T Instance => instance;
    protected virtual void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(gameObject);
            return;
        }
        instance = this as T;
        DontDestroyOnLoad(gameObject);
    }

    protected virtual void OnDestroy()
    {
        if (instance == this)
        {
            instance = null;
        }
    }
}

七、OnDestroy 清除 Instance

复制代码
protected virtual void OnDestroy()
{
    if (instance == this)
    {
        instance = null;
    }
}

作用:当前真正的单例被销毁时,同时清空静态引用。

让单例状态和实际对象生命周期保持一致。注意一定要判断:

复制代码
instance == this

避免重复对象销毁时把真正的单例引用清掉。

八、自动创建单例的重复问题

如果使用自动创建方式:

复制代码
if (instance == null)
{
    GameObject obj = new GameObject(typeof(T).Name);
    instance = obj.AddComponent<T>();
    DontDestroyOnLoad(obj);
}

核心规则:自动创建的单例就不要再手动放进场景。

九、单例重复挂载的处理方式

第一层DisallowMultipleComponent

第二层Awake 中判断 Instance

相关推荐
quantdash_cc15 分钟前
批量请求和循环请求有什么区别?从 API 请求次数看量化数据获取的工程设计
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
qq_4069810321 分钟前
大白话rust系列01注释
开发语言·rust
橘子编程21 分钟前
Java邮件发送全攻略:从入门到实战
java·开发语言·spring boot·spring·spring cloud·maven
Tizzy JJ32 分钟前
Python + pytest 接口自动化测试框架实战:从零搭建企业级项目骨架
开发语言·python·pytest
海兰33 分钟前
【 Python 量化交易】第8章:量化工具箱
开发语言·python
牛油果子哥q34 分钟前
C++大型项目工程精讲:CMake完整实战、静态库&动态库、模块化拆分、单元测试、gdb调试、性能工具、工程踩坑全解
开发语言·c++·单元测试
Dream Cosmos36 分钟前
C++ 多态上篇:从 virtual 到抽象类,彻底理解多态的使用
开发语言·c++
TheBestRucy37 分钟前
Python九阳神功之柒:数据分析三剑客·执剑问道
开发语言·python·数据分析
qq_339191141 小时前
go cpu占比高排查,cpu100%排查,go pprof cpu命令
开发语言·后端·golang