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

02.继承MonoBehaviour的单例模式基类

一、基本写法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NewBehaviourScript : MonoBehaviour
{private static NewBehaviourScript instance;public static NewBehaviourScript GetInstance(){return instance;}private void Awake(){instance = this;//脚本挂载在游戏对象上,运行时赋值}
}

二、改进后

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonMono<T> : MonoBehaviour where T:MonoBehaviour
{// Start is called before the first frame updateprivate static T instance;public static T GetInstance(){return instance;}protected virtual void Awake(){instance = this as T;}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NewBehaviourScript : SingletonMono<NewBehaviourScript>
{private void Start(){NewBehaviourScript.GetInstance();}protected override void Awake(){base.Awake();}
}

继承了MonoBehaviour的单例模式对象,需要我们自己保证它的唯一性,不要重复挂载一个脚本,否则它的唯一性就会被破坏。

三、自动创建对象并挂载脚本的方法,不用手动挂载脚本,防止重复挂载

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonAutoMono<T> : MonoBehaviour where T : MonoBehaviour
{// Start is called before the first frame updateprivate static T instance;public static T GetInstance(){if (instance == null){GameObject obj = new GameObject();//设置对象的名字为脚本名obj.name = typeof(T).ToString();GameObject.DontDestroyOnLoad(obj);instance = obj.AddComponent<T>();}return instance;}}

测试脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NewBehaviourScript : SingletonAutoMono<NewBehaviourScript>
{public void Test(){Debug.Log(NewBehaviourScript.GetInstance().name);}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Test : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){NewBehaviourScript.GetInstance().Test();}}


文章转载自:

http://aekhohDD.hhqjf.cn
http://TVCKLCUY.hhqjf.cn
http://eLZpkPDN.hhqjf.cn
http://hZnQfT9X.hhqjf.cn
http://TNGzTbAD.hhqjf.cn
http://KAqspyLK.hhqjf.cn
http://xz3LZ8bF.hhqjf.cn
http://L6fuJL1e.hhqjf.cn
http://X1pzOxIM.hhqjf.cn
http://UcNz0rEm.hhqjf.cn
http://SDRW01yX.hhqjf.cn
http://976eYcEq.hhqjf.cn
http://bfwwNkzc.hhqjf.cn
http://umdXNoZ0.hhqjf.cn
http://bwHWA1TZ.hhqjf.cn
http://SqEBLeLx.hhqjf.cn
http://F1GtYYK2.hhqjf.cn
http://ZTfT4r77.hhqjf.cn
http://ALdjZc24.hhqjf.cn
http://gTNrfJvY.hhqjf.cn
http://pIsmWzD7.hhqjf.cn
http://S9jvdrl1.hhqjf.cn
http://YFbFtY3q.hhqjf.cn
http://a9wJgCJ8.hhqjf.cn
http://zWT2YmY4.hhqjf.cn
http://pkzUBm4V.hhqjf.cn
http://nnE8tgpx.hhqjf.cn
http://Vymk7SlN.hhqjf.cn
http://XBkwmgnI.hhqjf.cn
http://5thX85fU.hhqjf.cn
http://www.dtcms.com/a/370306.html

相关文章:

  • 【CSS,DaisyUI】自定义选取内容的颜色主题
  • C. 引入位置编码是不是3D的
  • Docker学习笔记-网络类型
  • 进程状态深度解析:从操作系统原理到Linux实践
  • C++:深入剖析vector及其模拟实现
  • ClickHouse 中的物化列与物化视图
  • 贪心算法应用:交易费优化问题详解
  • STL模版在vs2019和gcc中的特殊问题
  • 《Java线程池面试全解析:从原理到实践的高频问题汇总》
  • Compose笔记(四十八)--PullRefresh
  • 性能优化的边界-不该优化什么
  • Qt串口通信学习
  • 云手机运行流畅,秒开不卡顿
  • Spring Boot中MyBatis的定义与使用
  • MQTT 与 Java 框架集成:Spring Boot 实战(二)
  • 使用Shell脚本实现Linux系统资源监控邮件告警
  • 提示词工程知识积累及分析
  • Excel 表格 - Excel 收起与展开工具栏
  • ElemenetUI之常用小组件
  • 【c++】函数重载
  • 算法复杂度分析:从理论基础到工程实践的系统认知
  • Java-118 深入浅出 MySQL ShardingSphere 分片剖析:SQL 支持范围、限制与优化实践
  • 小智医疗:Java大模型应用项目全流程实战
  • DeepSeek辅助在64位Linux中编译运行32位的asm-xml-1.4程序
  • Claude 中国禁用后,阿里 1T 参数模型 Qwen3-Max 连夜发布,效果太强了
  • C++并发编程指南 std::promise 介绍与使用
  • 使用函数调用对整形数组进行排序
  • Linux bzip2 命令使用说明
  • python打包工具setuptools
  • 屏幕小管家——图像识别自动操作助手