当前位置:网站首页>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();
}
}边栏推荐
- js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
- Jerry's watch stops ringing [article]
- Direct control PTZ PTZ PTZ PTZ camera debugging (c)
- Js1day (syntaxe d'entrée / sortie, type de données, conversion de type de données, Var et let différenciés)
- 获取文件版权信息
- spfa AcWing 851. SPFA finding the shortest path
- Unity SKFramework框架(十四)、Extension 扩展函数
- Floyd AcWing 854. Floyd finds the shortest path
- . Net wechat message template push
- [opencv] [image gradient]
猜你喜欢

Heap acwing 838 Heap sort

国产免费数据仓库ETL调度自动化运维专家—TASKCTL

Linear DP acwing 896 Longest ascending subsequence II

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

VIM super practical guide collection of this one is enough

挥发性有机物TVOC、VOC、VOCS气体检测+解决方案

JS iterator generator asynchronous code processing promise+ generator - > await/async

C#修饰符

Dijkstra AcWing 850. Dijkstra finding the shortest circuit II

js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
随机推荐
Linear DP acwing 898 Number triangle
C#修饰符
Jerry's watch stops ringing [article]
VIM super practical guide collection of this one is enough
Do you know all the interface test interview questions?
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
[opencv learning] [Canny edge detection]
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
How can attribute mapping of entity classes be without it?
Obtain file copyright information
Modular commonjs es module
Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk
bellman-ford AcWing 853. Shortest path with side limit
三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难
Apply lnk306gn-tl converter, non isolated power supply
国产免费数据仓库ETL调度自动化运维专家—TASKCTL
嵌入式软件开发
js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)
Structured data, semi-structured data and unstructured data
Traverse entrylist method correctly