当前位置:网站首页>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();
}
}
边栏推荐
- C#运算符
- 8A Synchronous Step-Down regulator tps568230rjer_ Specification information
- Interval DP acwing 282 Stone merging
- LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
- js1day(輸入輸出語法,數據類型,數據類型轉換,var和let區別)
- [opencv learning] [contour detection]
- JDBC prevent SQL injection problems and solutions [preparedstatement]
- Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
- js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)
- The UVM Primer——Chapter2: A Conventional Testbench for the TinyALU
猜你喜欢
Word efficiency guide - word's own template
[opencv learning] [moving object detection]
C#运算符
Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures in detail! A little six
上海交大教授:何援军——包围盒(包容体/包围盒子)
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
Get started REPORT | today, talk about the microservice architecture currently used by Tencent
net share
Everyone wants to eat a broken buffet. It's almost cold
Async/await asynchronous function
随机推荐
一些突然迸发出的程序思想(模块化处理)
Some sudden program ideas (modular processing)
C modifier
Analog to digital converter (ADC) ade7913ariz is specially designed for three-phase energy metering applications
8A Synchronous Step-Down regulator tps568230rjer_ Specification information
难忘阿里,4面技术5面HR附加笔试面,走的真艰难真心酸
Js1day (input / output syntax, data type, data type conversion, VaR and let differences)
Std:: vector batch import fast de duplication method
移动式布局(流式布局)
Jerry's weather direction coding table [chapter]
Async/await asynchronous function
JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
ASP. Net MVC default configuration, if any, jumps to the corresponding program in the specified area
Typora+docsify quick start
Fully autonomous and controllable 3D cloud CAD: crowncad's convenient command search can quickly locate the specific location of the required command.
基于STM32的OLED 屏幕驱动
Wechat official account payment prompt MCH_ ID parameter format error
Record idea shortcut keys
Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY