当前位置:网站首页>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();
}
}
边栏推荐
- NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
- Rust language document Lite (Part 1) - cargo, output, basic syntax, data type, ownership, structure, enumeration and pattern matching
- [200 opencv routines] 100 Adaptive local noise reduction filter
- 通过反射执行任意类的任意方法
- Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk
- About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
- Dijkstra AcWing 850. Dijkstra求最短路 II
- Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
- Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
- Interesting interview questions
猜你喜欢
Finally, someone explained the supervised learning clearly
Jerry's watch ringtone audition [article]
Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
Heap acwing 838 Heap sort
Floyd AcWing 854. Floyd finds the shortest path
难忘阿里,4面技术5面HR附加笔试面,走的真艰难真心酸
Js4day (DOM start: get DOM element content, modify element style, modify form element attributes, setinterval timer, carousel Map Case)
[error record] cannot open "XXX" because Apple cannot check whether it contains malware
Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
随机推荐
VIM super practical guide collection of this one is enough
Mongodb redis differences
Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk
绕过ObRegisterCallbacks需要驱动签名方法
Interesting interview questions
About the loading of layer web spring layer components, the position of the layer is centered
国产免费数据仓库ETL调度自动化运维专家—TASKCTL
Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
spfa AcWing 851. SPFA finding the shortest path
Finally, someone explained the supervised learning clearly
3 a VTT terminal regulator ncp51200mntxg data
PXE installation UOS prompt NFS over TCP not available from 10 x.x.x
To bypass obregistercallbacks, you need to drive the signature method
[opencv learning] [image filtering]
Jerry's watch modifies the alarm clock [chapter]
嵌入式软件开发
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
[opencv learning] [image histogram and equalization]
Efficiency comparison between ArrayList and LinkedList
Ntmfs4c05nt1g N-ch 30V 11.9a MOS tube, pdf