using System;

public class Singleton<T> where T : class, new()
{
private static T m_instance; public static T instance
{
get
{
if (Singleton<T>.m_instance == null)
{
Singleton<T>.CreateInstance();
}
return Singleton<T>.m_instance;
}
} protected Singleton()
{
} public static void CreateInstance()
{
if (Singleton<T>.m_instance == null)
{
Singleton<T>.m_instance = Activator.CreateInstance<T>();
(Singleton<T>.m_instance as Singleton<T>).Init();
}
} public static void DestroyInstance()
{
if (Singleton<T>.m_instance != null)
{
(Singleton<T>.m_instance as Singleton<T>).UnInit();
Singleton<T>.m_instance = (T)((object)null);
}
} public static T GetInstance()
{
if (Singleton<T>.m_instance == null)
{
Singleton<T>.CreateInstance();
}
return Singleton<T>.m_instance;
} public static bool HasInstance()
{
return Singleton<T>.m_instance != null;
} public virtual void Init()
{
} public virtual void UnInit()
{
}
}

  

using UnityEngine;

public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static bool applicationIsQuitting = false;
private static T _instance;
private static object _lock = new object(); public static T Instance
{
get
{
if (applicationIsQuitting)
{
Debug.LogWarning("[Singleton] Instance " + typeof(T) +
" already destroyed on application quit." +
"Won't create again - returning null.");
return null;
}
lock (_lock)
{
if (_instance == null)
{
_instance = (T)FindObjectOfType(typeof(T));
if (_instance == null)
{
GameObject singleton = new GameObject();
_instance = singleton.AddComponent<T>();
singleton.name = "(singleton) " + typeof(T).ToString();
DontDestroyOnLoad(singleton);
Debug.Log("[Singleton] An instance of " + typeof(T) +
" is needed in the scene, so '" + singleton +
"' was created with DontDestroyOnLoad.");
}
else
{
Debug.Log("[Singleton] Using instance already created: " +
_instance.gameObject.name);
}
}
return _instance;
}
}
} /////////////////////////////////////////// private void Awake()
{
Init();
} private void Update()
{
Tick();
}
private void OnDestroy()
{
applicationIsQuitting = true;
UnInit();
} /////////////////////////////////////////// protected virtual void Init()
{ } protected virtual void UnInit()
{ } protected virtual void Tick()
{ } }

  

Unity里面两种单例模式的实现的更多相关文章

  1. 7、java实现的两种单例模式

    /* 两种单例模式的演示 */ //饿汉式 class Signal { private Signal(){} private Signal s = new Signal(); public stat ...

  2. iOS开发笔记-两种单例模式的写法

    iOS开发笔记-两种单例模式的写法   单例模式是开发中最常用的写法之一,iOS的单例模式有两种官方写法,如下: 不使用GCD #import "ServiceManager.h" ...

  3. 关于Unity的两种调试方法

    Unity的两种调试方法 1.Debug.Log()输出语句调试,平时经常用这个 2.把MonoDevelop和Unity进行连接后断点调试 先把编辑器选择为MonoDevelop,Edit----& ...

  4. Unity中有两种Animation Clip

    http://blog.csdn.net/zzxiang1985/article/details/51291861 在Unity中,我们有两种方法创建Animation Clip. 一种(后面简称方法 ...

  5. java中两种单例模式

    //懒汉式(线程不安全) class LazySingleton{ private static LazySingleton singleton; private LazySingleton(){} ...

  6. [Swift实际操作]八、实用进阶-(1)Swift语言中的两种单例模式实际操作

    本文降温你解析常见的单例模式.单例模式可以保证一个类仅有一个实例,同时这个类还必须提供一个访问该类的全局访问点. 首先导入需要使用到的界面工具框架 import UIKit 单例对象保证了只有一个实例 ...

  7. Unity 2D两种常用判断点击的方法

    1.Raycast法 原理相同于3D中得Raycast法,具体使用略有区别. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorl ...

  8. unity 旋转两种方法

    transform.Rotate(new Vector3(0, 10, 10)*speed*Time.deltaTime); // 物体绕x轴.y轴.z轴旋转 transform.RotateArou ...

  9. angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用

    今天我们要讲的是ng2的路由系统. 例子

随机推荐

  1. 使用grunt中遇到的问题

    1.使用jshint进行代码检查时,grunt命令后报错: 因为出现了乱码,我猜测是因为编码原因导致的.遂在webstorm的setting中修改了编码为utf-8,问题解决.

  2. Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

  3. bzoj 1098 [POI2007]办公楼biu——链表

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1098 求补图的连通块大小.与自己没有边的和自己在一个连通块里. 用链表把所有点串起来.先给自 ...

  4. 表单提交Post方法、Get方法

     表单用来接受用户的输入,并将用户的输入以“name=value值对”集合的形式提交到服务器进行处理.那么表单是怎样将数据提交到服务器的?服务器是怎样对表单数据进行处理的?下面我将为大家揭开表单提交背 ...

  5. c语言里如何调用汇编里的变量?

    c语言里如何调用汇编里的变量? 汇编语言:是声明全局变量 .globl _end_ofs _end_ofs: .word _end - _start c语言:声明这个变量,然后再调用这个变量 void ...

  6. nohup command > myout.file 2>&1 &

    nohup command > myout.file 2>&1 &

  7. AngularJS系统学习之Directive(指令)

    本文转自https://www.w3ctech.com/topic/1612 原文作者: Nicolas Bevacqua 原文:AngularJS’ Internals In Depth, Part ...

  8. mfc画波形函数

    void CMyPicoTestDlg::DrawWave(CDC *pDC,CRect &rectPicture) { float fDeltaX; float fDeltaY; int n ...

  9. PLSQL导入导出oracle表 表空间

    PLSQL导入导出表的正确步骤 原来总是直接 tools->import talbes->Oracle Import结果发现有的时候会出错:有的表不能正确导入, baidu+googel解 ...

  10. LeetCode: 520 Detect Capital(easy)

    题目: Given a word, you need to judge whether the usage of capitals in it is right or not. We define t ...