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

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://nYo3ulb4.hLfsn.cn
http://Zx6aXqOg.hLfsn.cn
http://fBPE2f0x.hLfsn.cn
http://f8bbhir6.hLfsn.cn
http://oSir3fb3.hLfsn.cn
http://VYBY7qV6.hLfsn.cn
http://S3WSTk0A.hLfsn.cn
http://MyvD2JC8.hLfsn.cn
http://yjO10nSu.hLfsn.cn
http://H7hR8QkJ.hLfsn.cn
http://sTj55TGj.hLfsn.cn
http://MPcnJgNh.hLfsn.cn
http://lBr1XeMy.hLfsn.cn
http://kWf6yvqM.hLfsn.cn
http://cNQQ35lr.hLfsn.cn
http://Ov25Fldz.hLfsn.cn
http://q0fgckVo.hLfsn.cn
http://PqmhuWz0.hLfsn.cn
http://Iid4uPxn.hLfsn.cn
http://E5G1t7cU.hLfsn.cn
http://nMBaBhG9.hLfsn.cn
http://lGUOJS7K.hLfsn.cn
http://tqofSZxh.hLfsn.cn
http://fWainQUV.hLfsn.cn
http://9oxf2mIL.hLfsn.cn
http://AYjHWazd.hLfsn.cn
http://k3vLInl2.hLfsn.cn
http://fJ1PaZti.hLfsn.cn
http://3jcZbJBW.hLfsn.cn
http://QN65DnYL.hLfsn.cn
http://www.dtcms.com/a/227832.html

相关文章:

  • 回测效率提升500%!khQuant打板策略回测性能深度剖析——基于miniQMT的回测系统深度优化【AI量化第29篇】
  • AI矢量软件|Illustrator 2025网盘下载与安装教程指南
  • PAT-甲级JAVA题解(更新中...)
  • 浅写弱口令与命令爆破
  • 中科院报道铁电液晶:从实验室突破到多场景应用展望
  • 动中通天线跟踪性能指标的测试
  • windows11安装scoop 20250602
  • RAG入门 - Retriever(1)
  • 每天总结一个html标签——a标签
  • 第一章:计算机系统概论
  • AI智能体|扣子(Coze)搭建【合同/文档审查】工作流
  • Modern C++(二)预处理器及表达式
  • 嵌入式开发之STM32学习笔记day16
  • 安装DockerDocker-Compose
  • Playwright Python API 测试:从入门到实践
  • LearnOpenGL-笔记-其十三
  • ROS 2源换源后GPG错误解决方法
  • LangChain输出格式化实践:提升测试工程师LLM开发效率的完整指南
  • Python训练营---Day43
  • 云服务器突发宕机或无响应怎么办
  • <6>, 界面优化
  • 【PCB设计】STM32开发板——电源设计
  • 第二章支线五 ·CSS炼金续章:变量与暗黑主题术
  • 云服务器无法远程连接怎么办?
  • 动态规划(10):状态压缩
  • Flex弹性布局
  • 量子计算在大模型微调中的技术突破
  • 什么是线程上下文切换?
  • 推荐一款使用html开发桌面应用的工具——mixone
  • 描述性统计——让数据说话