当前位置:网站首页>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;
}
}
}
边栏推荐
- Unity typewriter teaches you three ways
- Prize essay solicitation | 2022 cloud native programming challenge draft activity opens
- Huawei cloud digital asset chain, "chain" connects the digital economy, infinite splendor
- JS fly into JS special effect pop-up login box
- JS drag and drop alert pop-up plug-in
- 华为云数字资产链,“链”接数字经济无限精彩
- 一文了解 Rainbond 云原生应用管理平台
- Space shooting Lesson 11: sound and music
- GIS数据漫谈(六)— 投影坐标系统
- Confusing knowledge points of software designer examination
猜你喜欢

JS chart scatter example

Unity performance optimization

卡通js射击小游戏源码

Redis的三种删除策略以及逐出算法

Easynlp Chinese text and image generation model takes you to become an artist in seconds

Unity uses shader to quickly make a circular mask

阿里云 MSE 支持 Go 语言流量防护
![[C language brush questions] explanation of linked list application](/img/44/1de4e3f0712780680fbdc904dfe39a.png)
[C language brush questions] explanation of linked list application

作业 ce

Three deletion strategies and eviction algorithm of redis
随机推荐
Understanding of C # delegate
flask 静态文件服务搭建
Alibaba cloud MSE supports go language traffic protection
微信小程序的分包加载
How can enterprises successfully complete cloud migration?
Oracle库访问很慢,是什么原因?
Laser slam:logo-loam --- code compilation, installation and gazebo test
PostgreSQL数据库删库前是不是需要把所有连接断开才能删除?
既要便捷、安全+智能,也要颜值,萤石发布北斗星人脸锁DL30F和极光人脸视频锁Y3000FV
Thinking and summary of R & D Efficiency
Subcontracting loading of wechat applet
Explain mesh Collider in unity
h5微信射击小游戏源码
JS fly into JS special effect pop-up login box
Hangao database best practice configuration tool Hg_ BP log collection content
Unity package exe to read and write excel table files
C # basic 3-value type and reference type, packing and unpacking
什么是“安全感”?沃尔沃用它自己独特的理解以及行动来告诉你
Space shooting Lesson 16: props (Part 2)
The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!