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

Unity3D 单例模式

Unity3D 泛型单例

单例模式

单例模式是一种创建型设计模式,能够保证一个类只有一个实例,提供访问实例的全局节点。

通常会把一些管理类设置成单例,例如 GameManagerUIManager 等,可以很方便地使用这些管理类单例,存储变量和调用接口。

手动挂载的泛型单例

创建 SingletonMono.cs 脚本,在类名后面添加泛型和约束,定义泛型变量,并且在 Awake 方法中对变量进行赋值。

这里的 Awake 方法是虚方法,当有管理类继承这个 SingletonMono 时,可以重写 Awake 方法进行额外的操作。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
{static T instance;  // 私有静态实例public static T Instance { get { return instance; } }  // 公开实例属性protected virtual void Awake(){if (instance == null){instance = this as T;// 切换场景时不销毁这个游戏物体DontDestroyOnLoad(gameObject);}else{// 切换场景时,如果场景里有单例游戏物体,在已经创建单例的情况下,销毁多余的游戏物体Destroy(gameObject);}}
}

创建 GameManager.cs 脚本,继承 SingletonMono 这个类。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class GameManager : SingletonMono<GameManager>
{public int score;protected override void Awake(){// 调用基类的 Awake 方法base.Awake();// 可以进行额外的初始化操作score = 0;}void Start(){}void Update(){}
}

在场景中创建游戏物体,把 GameManager 脚本手动挂载到游戏物体上。

手动挂载

创建 SingletonTest.cs 脚本,简单使用一下 GameManager.Instance 单例的变量。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonTest : MonoBehaviour
{void Start(){int score = GameManager.Instance.score;Debug.Log($"score = {score}");}
}

运行游戏,可以看到 GameManagerDontDestroyOnLoad 场景中,可以获取到 score 变量进行打印。

使用单例

自动挂载的泛型单例

创建 SingletonMonoAuto.cs 脚本,在类名后面添加泛型和约束,定义泛型变量。

因为它并不需要在场景中手动创建游戏物体,也不会通过 Awake 方法对变量进行赋值。

所以在获取 Instance 属性时,如果属性为空,就通过代码创建一个不会销毁的游戏物体,并自动挂载单例组件。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonMonoAuto<T> : MonoBehaviour where T : MonoBehaviour
{static T instance;  // 私有静态实例// 公开实例属性public static T Instance{get{if (instance == null){// 创建一个新的游戏物体GameObject obj = new GameObject();// 根据类型进行重命名obj.name = typeof(T).ToString();// 自动挂载单例组件instance = obj.AddComponent<T>();// 不可销毁DontDestroyOnLoad(obj);}// 返回实例return instance;}}
}

创建一个 UIManager.cs 脚本,继承 SingletonMonoAuto 这个类。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class UIManager : SingletonMonoAuto<UIManager>
{void Awake(){Debug.Log("初始化 UIManager");}void Start(){}void Update(){}
}

SingletonTest.cs 脚本,简单使用一下 UIManager.Instance 单例。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonTest : MonoBehaviour
{void Start(){int score = GameManager.Instance.score;Debug.Log($"score = {score}");UIManager uiManager = UIManager.Instance;}
}

运行游戏,可以看到 UIManagerDontDestroyOnLoad 场景中自动创建。

自动挂载

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

相关文章:

  • 解析TMalign文本文件中的转换矩阵
  • vue.js组建开发
  • D29【python 接口自动化学习】- python基础之输入输出与文件操作
  • jQuery——平滑翻页
  • 二叉树--DS
  • State of ChatGPT ---- ChatGPT的技术综述
  • 构建高效新闻推荐系统:Spring Boot的力量
  • 如何使用ipopt进行非线性约束求目标函数最小值(NLP非线性规划)内点法(inner point method)
  • 【Unity学习笔记】解决疑似升级Win11或使用Unity6导致Unity旧版本无法打开的问题
  • 回归分析在数据挖掘中的应用简析
  • 【Node.js】worker_threads 多线程
  • 贪心算法c++
  • 【STM32】 TCP/IP通信协议(3)--LwIP网络接口
  • 15分钟学 Python 第39天:Python 爬虫入门(五)
  • 使用Pytorch构建自定义层并在模型中使用
  • 学习记录:js算法(五十六):从前序与中序遍历序列构造二叉树
  • qt使用QDomDocument读写xml文件
  • Oracle架构之表空间详解
  • springboot整合seata
  • 鸿蒙开发(NEXT/API 12)【二次向用户申请授权】程序访问控制
  • docker export/import 和 docker save/load 的区别
  • 明星周边销售网站开发:SpringBoot技术全解析
  • STM32+ADC+扫描模式
  • R语言绘制散点图
  • 安装最新 MySQL 8.0 数据库(教学用)
  • 微信小程序开发-配置文件详解
  • TCP/UDP初识
  • 【大数据】在线分析、近线分析与离线分析
  • 【unity进阶知识9】序列化字典,场景,vector,color,Quaternion
  • 传奇GOM引擎架设好进游戏后提示请关闭非法外挂,重新登录,如何处理?