当前位置:网站首页>Unity skframework framework (XV), singleton singleton
Unity skframework framework (XV), singleton singleton
2022-07-02 13:07:00 【CoderZ1010】
Catalog
One 、 Common type single example
brief introduction
Whether the singleton inherits by type MonoBehaviour Divided into two kinds , They are single cases of common type Singleton and Mono Type singleton MonoSingleton
One 、 Common type single example
Common type singletons are implemented through inheritance ISingleton Interface and implement OnInit event , This event is called automatically when the singleton is initialized .
Suppose we have a class A, It contains some properties and methods
using UnityEngine;
public class A
{
public string StrValue { get; private set; }
public void Func()
{
Debug.Log("Singleton Example.");
}
}
By inheritance ISingleton Interface uses it as a singleton class
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.");
}
}
Invoke the sample :
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);
}
}
Release of single case :
// Case release sheet
Singleton<A>.Dispose();
Two 、Mono Type singleton
Mono A singleton of type is inherited IMonoSingleton Interface to implement ,IsDontDestroyOnLoad Attribute is used to specify whether the singleton object is not destroyed .
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.");
}
}
Invoke the sample :
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
private void Start()
{
MonoSingleton<A>.Instance.Func();
}
}
Release of single case :
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
private void Start()
{
MonoSingleton<A>.Instance.Func();
}
private void OnDestroy()
{
MonoSingleton<A>.Dispose();
}
}
边栏推荐
- PXE installation UOS prompt NFS over TCP not available from 10 x.x.x
- Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
- Jerry's watch gets the default ringtone selection list [article]
- Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
- 基于STM32的OLED 屏幕驱动
- Some sudden program ideas (modular processing)
- Apply lnk306gn-tl converter, non isolated power supply
- Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
- 三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难
- Direct control PTZ PTZ PTZ PTZ camera debugging (c)
猜你喜欢
Floyd AcWing 854. Floyd finds the shortest path
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
架构师必须了解的 5 种最佳软件架构模式
阿里发布的Redis开发文档,涵盖了所有的redis操作
Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures in detail! A little six
[200 opencv routines] 100 Adaptive local noise reduction filter
Js5day (event monitoring, function assignment to variables, callback function, environment object this, select all, invert selection cases, tab column cases)
Jerry's watch delete alarm clock [chapter]
ArrayList与LinkedList效率的对比
随机推荐
[opencv learning] [image filtering]
Linear DP acwing 897 Longest common subsequence
Jerry's watch reads the alarm clock [chapter]
阿里发布的Redis开发文档,涵盖了所有的redis操作
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
PR usage skills, how to use PR to watermark?
NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
js2day(又是i++和++i,if语句,三元运算符,switch、while语句,for循环语句)
(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
Jerry's watch ringtone audition [article]
3 A VTT端接 稳压器 NCP51200MNTXG资料
Ali was killed by two programming problems at the beginning, pushed inward again, and finally landed (he has taken an electronic offer)
Linear DP acwing 898 Number triangle
Apply lnk306gn-tl converter, non isolated power supply