当前位置:网站首页>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();
}
}边栏推荐
- js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
- Finally, someone explained the supervised learning clearly
- Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures in detail! A little six
- Browser storage scheme
- About asp Net MVC project in local vs running response time is too long to access, the solution!
- Redis introduction, scenario and data type
- Visual studio efficient and practical extension tools and plug-ins
- 自主可控三维云CAD:CrownCAD赋能企业创新设计
- Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy
- Interval DP acwing 282 Stone merging
猜你喜欢

Js2day (also i++ and ++i, if statements, ternary operators, switch, while statements, for loop statements)

国产免费数据仓库ETL调度自动化运维专家—TASKCTL
![Jerry's watch modifies the alarm clock [chapter]](/img/d6/04fb8143027578bb707529a05db548.jpg)
Jerry's watch modifies the alarm clock [chapter]

spfa AcWing 852. SPFA judgement negative ring

国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)

哈希表 AcWing 840. 模拟散列表
![JDBC 预防sql注入问题与解决方法[PreparedStatement]](/img/32/f71f5a31cdf710704267ff100b85d7.png)
JDBC 预防sql注入问题与解决方法[PreparedStatement]

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

Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk
![[200 opencv routines] 100 Adaptive local noise reduction filter](/img/89/9e9b667dd28cb25af005b6028ef26c.jpg)
[200 opencv routines] 100 Adaptive local noise reduction filter
随机推荐
三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难
[opencv learning] [moving object detection]
基于STM32的OLED 屏幕驱动
spfa AcWing 851. SPFA finding the shortest path
百款拿来就能用的网页特效,不来看看吗?
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
Post order traversal sequence of 24 binary search tree of sword finger offer
Linear DP acwing 899 Edit distance
Js2day (also i++ and ++i, if statements, ternary operators, switch, while statements, for loop statements)
完全自主可控三维云CAD:CrownCAD便捷的命令搜索,快速定位所需命令具体位置。
Dijkstra AcWing 850. Dijkstra求最短路 II
Linear DP acwing 898 Number triangle
Js1day (syntaxe d'entrée / sortie, type de données, conversion de type de données, Var et let différenciés)
. Net, C # basic knowledge
Jerry's watch reads the alarm clock [chapter]
JDBC 预防sql注入问题与解决方法[PreparedStatement]
NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
C#运算符
js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)