当前位置:网站首页>Introduction to singleton mode
Introduction to singleton mode
2022-07-28 20:55:00 【W.C.Zeng】
The singleton pattern
Reexamine the singleton pattern .
Singleton mode is the simplest design model , When we write a game management class GameManager when , We want only one instance of this class in memory , Save this instance through the static members of the class
With unity The inherited monobehavior Class GameManager Class, for example , You only need to save an instance of this class by saving a static member variable , Every time you want to generate a new GameManager Class time , If it already exists , Delete
Generic singleton pattern Singleton.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Generic singleton pattern Give Way MouseManager and GameManager All inherit this class
public class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
private static T instance;
public static T Instance
{
get {
return instance; }
}
protected virtual void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = (T)this;
}
}
public static bool IsInitialized
{
get {
return instance != null; }
}
protected virtual void OnDestory()
{
if (instance == this)
{
instance = null;
}
}
}
边栏推荐
- js图片悬挂样式照片墙js特效
- "When you are no longer a programmer, many things will get out of control" -- talk to SUSE CTO, the world's largest independent open source company
- Thinking and summary of R & D Efficiency
- 【1331. 数组序号转换】
- 全链路灰度在数据库上我们是怎么做的?
- 远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
- 阿里云 MSE 支持 Go 语言流量防护
- Explain prefabrication in unity in detail
- Leetcode:2141. The longest time to run n computers at the same time [the maximum value is two points]
- C reads the data in the CSV file and displays it after importing the DataTable
猜你喜欢

js图表散点图例子

Prize essay solicitation | 2022 cloud native programming challenge draft activity opens

有奖征文 | 2022 云原生编程挑战赛征稿活动开启

UE4 3dui widget translucent rendering blur and ghosting problems

《软件设计师考试》易混淆知识点

JS drag and drop alert pop-up plug-in

Introduction to redis II: RedHat 6.5 installation and use

JS page black and white background switch JS special effect

High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation

Unity uses shader to quickly make a circular mask
随机推荐
Easynlp Chinese text and image generation model takes you to become an artist in seconds
Looking at SQL optimization from the whole process of one query
Seventeen year operation and maintenance veterans, ten thousand words long, speak through the code of excellent maintenance and low cost~
太空游戏第12课: 盾牌
“当你不再是程序员,很多事会脱离掌控”—— 对话全球最大独立开源公司SUSE CTO...
Unity package exe to read and write excel table files
Redis review summary
The average altitude is 4000 meters! We built a cloud on the roof of the world
Interesting pictures and words
到底为什么不建议使用SELECT * ?
Prize essay solicitation | 2022 cloud native programming challenge draft activity opens
Yyds dry inventory interview must brush top101: every k nodes in the linked list are turned over
Use order by to sort
[server data recovery] HP StorageWorks series storage RAID5 two disk failure offline data recovery case
《软件设计师考试》易混淆知识点
Beautiful blue background form input box style
Space shooting lesson 14: player life
Sorting out problems in interface endpoint testing practice using Supertest
Introduction to redis I: redis practical reading notes
Lvs+keepalived high availability deployment practical application