当前位置:网站首页>unity-singleton mode
unity-singleton mode
2022-08-07 22:03:00 【Really fine duck】
Unity Singleton Pattern-Directory
What is singleton mode
Singleton pattern is a common software design pattern, which means that a class has only one instance and provides a globally accessible property to access this singleton.
So before using the singleton pattern, we have to think about how to get an instance?If we want to access another script in a C# script, we need to create it when we get the instance. If we want to call it in multiple scripts, the creation and destruction of instance objects will be very frequent, which will take up a lot of system resources.
Singleton can solve this problem. Singleton mode can directly create an instance to call globally, which is quite a globally shared class.Save system resources.
Benefits
- Can effectively reduce memory overhead, singleton mode has only one instance, avoid frequent creation and destruction of objects, save system resources
- Easy to call and share resources because a globally shared class is created
- Avoid resource duplication, such as a file write operation, since only one instance exists in memory, avoid simultaneous write operations to the same resource file
Disadvantages
- Singleton mode generally has no interface, it is very difficult for him to achieve extension
- All classes can be accessed, which will cause confusion in class relationships
- The code is bloated, reducing the readability and reliability of the code
Note: In the project, the dependency of the class on the singleton class should be reduced and the code quality should be improved
Script example
GameManager usually uses singleton mode in games. Let's use it as an example to show how to implement singleton mode in C# script
using System.Collections;using System.Collections.Generic;using UnityEngine;public class GameManager : MonoBehaviour{public static GameManager instance;private void Awake(){if(instance!=null){Destroy(gameObject);}instance = this;}}边栏推荐
- 同花顺开户靠谱吗?开户安全吗
- golang映射map详解:map的三种创建方式、map的增删改查
- [St. Regis Takeaway] day03: Improve the login function and add new employees
- 阿德的感悟
- 台式电脑亮度怎么调整 台式电脑亮度在哪里调
- Mind quantum 基础知识学习笔记(1)
- [笔记]攻防工具分享之 CobaltStrike框架 《一》环境配置
- leetcode 232. Implement Queue using Stacks
- 解决执行Command报错exit status 255
- win7不显示文件扩展名怎么办 win7显示文件扩展名方法
猜你喜欢
随机推荐
【 kali - elevated privileges 】 (4.2.2) social engineering toolkit: cloning fishing web site
笔记本怎样手写 笔记本电脑如何使用手写功能
Unity编辑器拓展--自定义Window拓展
用户信息管理系统项目测试
UE4 Sequence adds basic animation effects (01-object movement)
What risk fund accounts?Handle the fund account the safest?
LeetCode_Double Pointer_Medium_633. Sum of Squares
Unity编辑器拓展--预览窗口拓展
数据导入从关系型数据库导入
动手学深度学习_目标检测
打开方式默认怎么修改 怎么更改文件的打开方式
The solution to the prompt 0xc0000022 when playing some old games in win10 system.
如果Controller里有私有的方法,能成功访问吗?
《最新开源 随插即用》SAM 自增强注意力深度解读与实践(附代码及分析)
tensorflow/serving部署keras的h5模型服务
新电脑如何分区硬盘 新电脑分区怎么分区
复印机扫描功能怎么用 复印机里面的扫描怎么使用
[论文笔记] A Comprehensive Study on Learning-Based PE Malware Family Classification Methods
低成本、大容量、高交互…Polkadot 引领 GameFi 实现新突破
【json】- VScode中的扩展程序配置文件setting.json出错——Property expected和End of file expected

![[笔记]攻防工具分享之 CobaltStrike框架 《一》环境配置](/img/d8/9983846192821000b84d68300afa68.png)







