当前位置:网站首页>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;
}
}
}
边栏推荐
- 一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
- Why on earth is it not recommended to use select *?
- The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
- 云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
- Establishment of flask static file service
- Hangao database best practice configuration tool Hg_ BP log collection content
- View the thread stack according to the lwtid of opengauss/mogdb.
- High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation
- System. ArgumentException: Object of type ‘System. Int64‘ cannot be converted to type ‘System.Int32‘
- Unity gets which button (toggle) is selected under togglegroup
猜你喜欢

Why on earth is it not recommended to use select *?

js图片悬挂样式照片墙js特效

既要便捷、安全+智能,也要颜值,萤石发布北斗星人脸锁DL30F和极光人脸视频锁Y3000FV

Space shooting Lesson 13: explosion effect

卡通js射击小游戏源码

Explain mesh Collider in unity

Alibaba cloud MSE supports go language traffic protection

JS drag and drop alert pop-up plug-in
Looking at SQL optimization from the whole process of one query

JS win7 transparent desktop switching background start menu JS special effect
随机推荐
研发效能的思考总结
Huawei cloud digital asset chain, "chain" connects the digital economy, infinite splendor
Explain the mobile control implementation of unity in detail
The 678th operation
一文了解 Rainbond 云原生应用管理平台
Alibaba cloud MSE supports go language traffic protection
Database tuning - connection pool optimization
Cartoon JS shooting game source code
Laser slam:logo-loam --- code compilation, installation and gazebo test
How can enterprises successfully complete cloud migration?
Teach you unity scene switching progress bar production hand in hand
Explain prefabrication in unity in detail
记一次Runtime.getRuntime().exce(“command“)报错
7/27 training log (bit operation + suffix array)
Want to draw a picture that belongs to you? AI painting, you can also
How to use redis to realize things and locks?
JS picture hanging style photo wall JS special effect
Use order by to sort
How bad can a programmer be? Nima, they are all talents
Pl515 SOT23-5 single / Dual Port USB charging protocol port controller Parkson electronic agent