当前位置: 首页 > 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://www.lqws.cn/news/81883.html

相关文章:

  • Day10
  • leetcode刷题日记——二叉树的层次遍历
  • 全文索引详解及适用场景分析
  • 【Unity】云渲染
  • Ubuntu22.04 安装 CUDA12.8
  • 为什么ping显示connect:network is unreachable,如何排查网络不通问题?
  • 【数学 逆序对 构造】P12386 [蓝桥杯 2023 省 Python B] 混乱的数组|普及+
  • HTTP、WebSocket、SSE 对比
  • py爬虫的话,selenium是不是能完全取代requests?
  • 【Spring底层分析】Spring AOP基本使用+万字底层源码阅读分析
  • 使用 So-VITS-SVC 实现明星声音克隆与视频音轨替换实战全流程
  • windows11安装编译QtMvvm
  • Qt/C++编写GB28181服务端工具/绿色版开箱即用/对标wvp-gb28181/实时画面预览/录像回放下载
  • pikachu靶场通关笔记10 XSS关卡06-XSS之盲打
  • 结构型设计模式之装饰模式
  • C++string1号
  • NodeJS全栈WEB3面试题——P1基础知识:区块链与Web3原理
  • 腾答知识竞赛系统功能介绍
  • 【学习笔记】On the Biology of a Large Language Model
  • 《Effective Python》第六章 推导式和生成器——使用 yield from 组合多个生成器
  • 缓解颈部不适的营养补给之道
  • 线程池详细解析(二)
  • DAY 41 超大力王爱学Python
  • 5.29 自学测试 Linux基础 Day4
  • 由浅入深一文详解同余原理
  • SQL 窗口函数深度解析:ROW_NUMBER 实战指南
  • docker运行程序Killed异常排查
  • Node.js 项目调试指南
  • SOC-ESP32S3部分:25-HTTP请求
  • 初识CSS3