using UnityEngine;

 public class CommSystem: SingletonGeneric<CommSystem>
{
public static string version = "v1.0";
private static CommSystemStub stub = (new GameObject("CommSystemStub")).AddComponent<CommSystemStub>();
} public sealed class CommSystemStub : MonoBehaviour
{
private void Awake()
{
DontDestroyOnLoad(gameObject);
} private void Start()
{ } private void Update()
{ } private void OnDestroy()
{ }
}
CommSystem是普通的Class
 public class SingletonGeneric<T> where T: class, new()
{
static private T _instance;
static private readonly object th_safe = new object();
static public T Instance
{
get
{
if (_instance == null)
{
lock (th_safe)
{
if (_instance == null)
_instance = new T();
}
}
return _instance;
}
}
}

下面的改写让泛型单例支持使用Private的构造函数,这样做更严谨:

 using System;

 public class SingletonGeneric<T> where T: class
{
static private T _instance;
static private readonly object th_safe = new object();
static public T Instance
{
get
{
if (_instance == null)
{
lock (th_safe)
{
if (_instance == null)
//_instance = new T();
_instance = (T) Activator.CreateInstance(typeof(T), true);
}
}
return _instance;
}
}
}

MonoBehaviour单例的另外一种省事的写法的更多相关文章

  1. iOS:创建单例对象的两种方式

    单例模式:创建单例对象的两种方式 方式一:iOS4版本之前      static SingleClassManager *singleManager = nil;      +(SingleClas ...

  2. Unity 游戏框架搭建 (三) MonoBehaviour单例的模板

      上一篇文章讲述了如何设计C#单例的模板.也随之抛出了问题: 如何设计接收MonoBehaviour生命周期的单例的模板? 如何设计? 先分析下需求:   1.约束脚本实例对象的个数.   2.约束 ...

  3. 关于python单例的常用几种实现方法

    这两天在看自己之前写的代码,所以正好把用过的东西整理一下,单例模式,在日常的代码工作中也是经常被用到, 所以这里把之前用过的不同方式实现的单例方式整理一下 装饰器的方式 这种方式也是工作中经常用的一种 ...

  4. C# 创建单例你会几种方式?

    关于为什么需要创建单例?这里不过多介绍,具体百度知. 关于C#  创建单例步骤或条件吧 1.声明静态变量:2.私有构造函数(无法实例化)3.静态创建实例的方法:至于我这里的Singleton是seal ...

  5. iOS中类单例方法的一种实现

    在Cocos2D编程中,很多情况我们需要类只生成一个实例,这称之为该类的单例类. 一般我们在类中这样实现单例方法: +(instancetype)sharedInstance{ static Foo ...

  6. eval、exec及元类、单例实现的5种方法

    eval内置函数 # eval内置函数的使用场景:#   1.执行字符串会得到相应的执行结果#   2.一般用于类型转化,该函数执行完有返回值,得到dict.list.tuple等​dic_str = ...

  7. java单例类的几种实现

    一,最简单的方式 public class Singleton{ private Singleton(){}; private static Singleton instance = new Sing ...

  8. swift单例创建的几种方法

    //单例方法1 class SingleTonOne{ static var sharedInstanceOne:SingleTonOne{ struct SingleTonStruct { stat ...

  9. Java中 单例(Singleton)的两种方式

    第一种(饿汉式单例模式):在声明变量时实例化 public class Singleton { //静态初始化自动实例化 private static Singleton instance = new ...

随机推荐

  1. pads无模命令

    W<n>………改变线宽,比如 30. 栅格(Grids) G<xx>………过孔和设计栅格设置.GD<xx>………显示栅格设置.GP………打开或关闭极性栅格.GP r ...

  2. Balanced Lineup POJ - 3264

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  3. Wannafly Winter Camp 2020 Day 6C 酒馆战棋 - 贪心

    你方有 \(n\) 个人,攻击力和血量都是 \(1\).对方有 \(a\) 个普通人, \(b\) 个只有盾的,\(c\) 个只有嘲讽的,\(d\) 个有盾又有嘲讽的,他们的攻击力和血量都是无穷大.有 ...

  4. shadow文件中密码的加密方式

    1) 查看shadow文件的内容 cat /etc/shadow 可以得到shadow文件的内容,限于篇幅,我们举例说明: root:$1$Bg1H/4mz$X89TqH7tpi9dX1B9j5YsF ...

  5. Java-算式填符号

    题目: 某批警察叔叔正在进行智力训练:1 2 3 4 5 6 7 8 9 = 110 请看上边的算式,为了使等式成立,需要在数字间填入加号或者减号(可以不填,但不能填入其它符号).之间没有填入符号的数 ...

  6. C# IO流与文件读写学习笔记

    本笔记摘抄自:https://www.cnblogs.com/liyangLife/p/4797583.html,记录一下学习过程以备后续查用. 一.文件系统 1.1文件系统类的介绍 文件操作类大都在 ...

  7. 树莓派pip安装opencv报错,Could not find a version that satisfies the requirement cv2 (from versions: )No matching distribution found for cv2

    前言 我在使用pip install opencv-python 时报错 Could not find a version that satisfies the requirement opencv ...

  8. day5 字典 dict

    数据类型划分:可变数据类型,不可变数据类型不可变数据类型:元组,数字,布尔,str, == 可哈希可变数据类型:list , dict, set == 不可哈希dict的 key 必须是不可变数据类型 ...

  9. js 时间格式转换

    js时间格式转换 格式化时间转成时间戳 //格式化转时间戳(单位秒) function strtotime(strtime) { strtime = strtime.substring(0, 19); ...

  10. oracle卸载步骤

    一.删除产品 1. 停服务,进入下图,一个个删除Oracle产品,再执行卸载 二.清注册表,开始-搜索- 输入regedit,进入注册表 1.选择HKEY_LOCAL_MACHINE\SOFTWARE ...