当前位置: 首页 > article >正文

Unity中的MonoSingleton<T>与Singleton<T>

1.MonoSingleton

代码部分

using UnityEngine;/// <summary>
/// MonoBehaviour单例基类
/// 需要挂载到GameObject上使用
/// </summary>
public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{private static T _instance;private static readonly object _lock = new object();private static bool _applicationIsQuitting = false;public static T Instance{get{if (_applicationIsQuitting){Debug.LogWarning($"[MonoSingleton] Instance '{typeof(T)}' already destroyed on application quit. Won't create again.");return null;}lock (_lock){if (_instance == null){_instance = FindObjectOfType<T>();if (_instance == null){GameObject singletonObject = new GameObject();_instance = singletonObject.AddComponent<T>();singletonObject.name = typeof(T).ToString() + " (Singleton)";// 可选:让单例对象在场景切换时不被销毁//DontDestroyOnLoad(singletonObject);}}return _instance;}}}protected virtual void Awake(){if (_instance == null){_instance = this as T;//DontDestroyOnLoad(gameObject);}else if (_instance != this){Debug.LogWarning($"Another instance of {typeof(T)} already exists. Destroying this one.");Destroy(gameObject);}}protected virtual void OnApplicationQuit(){_applicationIsQuitting = true;}protected virtual void OnDestroy(){if (_instance == this){_instance = null;}}
}

说明

继承MonoSingleton的物体需要能挂载到场景物体上,即继承MonoBehaviour

我在MonoSingleton中将DontDestroyOnLoad(singletonObject)取消掉了,如果需要跨场景的话需要在继承的脚本中重写Awake方法

protected override void Awake()
{base.Awake();DontDestroyOnLoad(gameObject);
}

2.Singleton

代码部分

/// <summary>
/// 纯C#单例基类
/// 不需要挂载到GameObject上,可以直接调用
/// </summary>
public class Singleton<T> where T : class, new()
{private static T _instance;private static readonly object _lock = new object();public static T Instance{get{if (_instance == null){lock (_lock){if (_instance == null){_instance = new T();}}}return _instance;}}protected Singleton() { }
}

总结

本文的MonoSingleton与Singleton说明

MonoSingleton<T> 特点:

  • 需要挂载到GameObject上,继承MonoBehaviour
  • 线程安全的懒加载模式
  • 需要手动选择处理场景切换时的持久化(DontDestroyOnLoad)
  • 防止重复实例创建
  • 应用退出时的安全处理

Singleton<T> 特点:

  • 纯C#类,不需要挂载到GameObject
  • 线程安全的懒加载模式
  • 可以直接调用,无需场景依赖
  • 适合数据管理、配置管理等不需要MonoBehaviour生命周期的功能

都是很经典的框架,不多做说明

http://www.lryc.cn/news/2402386.html

相关文章:

  • 怎么通过 jvmti 去 hook java 层函数
  • 兰亭妙微 | 医疗软件的界面设计能有多专业?
  • 前端原生构建交互式进度步骤组件(Progress Steps)
  • 如何给windos11 扩大C盘容量
  • 【基于阿里云搭建数据仓库(离线)】Data Studio创建资源与函数
  • Linux_T(Sticky Bit)粘滞位详解
  • web3-以太坊智能合约基础(理解智能合约Solidity)
  • 高敏感应用如何保护自身不被逆向?iOS 安全加固策略与工具组合实战(含 Ipa Guard 等)
  • 【C++项目】负载均衡在线OJ系统-2
  • GC1809:高性能24bit/192kHz音频接收芯片解析
  • 2025年06月05日Github流行趋势
  • flask功能使用总结和完整示例
  • AWS 亚马逊 S3存储桶直传 前端demo 复制即可使用
  • DAY 15 复习日
  • Vue Router 导航方法完全指南
  • MidJourney入门学习
  • 2025最新Java日志框架深度解析:Log4j 2 vs Logback性能实测+企业级实战案例
  • 如何安全高效的文件管理?文件管理方法
  • 基于BI PaaS架构的衡石HENGSHI SENSE平台技术解析:重塑企业级数据分析基座
  • Hive中ORC存储格式的优化方法
  • 代码训练LeetCode(23)随机访问元素
  • 【R语言编程绘图-plotly】
  • float、double 这类 浮点数 相比,DECIMAL 是另一种完全不同的数值类型
  • 通信刚需,AI联手ethernet/ip转profinet网关打通工业技术难关
  • JavaEE->多线程:定时器
  • 6个月Python学习计划 Day 15 - 函数式编程、高阶函数、生成器/迭代器
  • <el-table>构建树形结构
  • linux——磁盘和文件系统管理
  • 云原生 DevOps 实践路线:构建敏捷、高效、可观测的交付体系
  • gateway 网关 路由新增 (已亲测)