当前位置:网站首页>Unity SKFramework框架(十五)、Singleton 单例
Unity SKFramework框架(十五)、Singleton 单例
2022-07-02 09:45:00 【CoderZ1010】
目录
简介
单例通过类型是否继承MonoBehaviour被区分为两种,分别是普通类型单例Singleton和Mono类型单例MonoSingleton
一、普通类型单例
普通类型单例的实现通过继承ISingleton接口并实现其中的OnInit事件,该事件在单例初始化时自动调用。

假设我们有一个类A,它包含一些属性和方法
using UnityEngine;
public class A
{
public string StrValue { get; private set; }
public void Func()
{
Debug.Log("Singleton Example.");
}
}通过继承ISingleton接口将其作为单例类使用
using UnityEngine;
using SK.Framework;
public class A : ISingleton
{
public string StrValue { get; private set; }
public void OnInit()
{
StrValue = "Test";
}
public void Func()
{
Debug.Log("Singleton Example.");
}
}调用示例:
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
private void Start()
{
string s = Singleton<A>.Instance.StrValue;
Singleton<A>.Instance.Func();
Debug.Log(s);
}
}
单例的释放:
//释放单例
Singleton<A>.Dispose();二、Mono类型单例
Mono类型的单例通过继承IMonoSingleton接口来实现,IsDontDestroyOnLoad属性用于指定该单例物体是否作不销毁处理。

using UnityEngine;
using SK.Framework;
public class A : MonoBehaviour, IMonoSingleton
{
public string StrValue { get; private set; }
public bool IsDontDestroyOnLoad { get { return true; } }
public void OnInit()
{
StrValue = "Test";
}
public void Func()
{
Debug.Log("Singleton Example.");
}
}调用示例:
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
private void Start()
{
MonoSingleton<A>.Instance.Func();
}
}
单例的释放:
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
private void Start()
{
MonoSingleton<A>.Instance.Func();
}
private void OnDestroy()
{
MonoSingleton<A>.Dispose();
}
}边栏推荐
- ArrayList与LinkedList效率的对比
- 难忘阿里,4面技术5面HR附加笔试面,走的真艰难真心酸
- VIM super practical guide collection of this one is enough
- ASP. Net MVC default configuration, if any, jumps to the corresponding program in the specified area
- Jerry's watch ringtone audition [article]
- js1day(輸入輸出語法,數據類型,數據類型轉換,var和let區別)
- 3 A VTT端接 稳压器 NCP51200MNTXG资料
- 堆 AcWing 839. 模拟堆
- JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
- Linear DP acwing 898 Number triangle
猜你喜欢

The coloring method determines the bipartite graph acwing 860 Chromatic judgement bipartite graph

Get started REPORT | today, talk about the microservice architecture currently used by Tencent

Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本

3 a VTT terminal regulator ncp51200mntxg data

C modifier

Unity SKFramework框架(十九)、POI 兴趣点/信息点

JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)

NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF

js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)

(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
随机推荐
JDBC prevent SQL injection problems and solutions [preparedstatement]
Js3day (array operation, JS bubble sort, function, debug window, scope and scope chain, anonymous function, object, Math object)
Unity SKFramework框架(十九)、POI 兴趣点/信息点
[opencv] [image gradient]
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
Js2day (also i++ and ++i, if statements, ternary operators, switch, while statements, for loop statements)
Fully autonomous and controllable 3D cloud CAD: crowncad's convenient command search can quickly locate the specific location of the required command.
Variable, "+" sign, data type
Mui WebView down refresh pull-up load implementation
Linear DP acwing 897 Longest common subsequence
js1day(輸入輸出語法,數據類型,數據類型轉換,var和let區別)
[opencv learning] [common image convolution kernel]
传感器 ADXL335BCPZ-RL7 3轴 加速度计 符合 RoHS/WEEE
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
Async/await asynchronous function
Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
Typora+docsify quick start
Finally, someone explained the supervised learning clearly
8 examples of using date commands
Linear DP acwing 899 Edit distance