Unity里面两种单例模式的实现
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里面两种单例模式的实现的更多相关文章
- 7、java实现的两种单例模式
/* 两种单例模式的演示 */ //饿汉式 class Signal { private Signal(){} private Signal s = new Signal(); public stat ...
- iOS开发笔记-两种单例模式的写法
iOS开发笔记-两种单例模式的写法 单例模式是开发中最常用的写法之一,iOS的单例模式有两种官方写法,如下: 不使用GCD #import "ServiceManager.h" ...
- 关于Unity的两种调试方法
Unity的两种调试方法 1.Debug.Log()输出语句调试,平时经常用这个 2.把MonoDevelop和Unity进行连接后断点调试 先把编辑器选择为MonoDevelop,Edit----& ...
- Unity中有两种Animation Clip
http://blog.csdn.net/zzxiang1985/article/details/51291861 在Unity中,我们有两种方法创建Animation Clip. 一种(后面简称方法 ...
- java中两种单例模式
//懒汉式(线程不安全) class LazySingleton{ private static LazySingleton singleton; private LazySingleton(){} ...
- [Swift实际操作]八、实用进阶-(1)Swift语言中的两种单例模式实际操作
本文降温你解析常见的单例模式.单例模式可以保证一个类仅有一个实例,同时这个类还必须提供一个访问该类的全局访问点. 首先导入需要使用到的界面工具框架 import UIKit 单例对象保证了只有一个实例 ...
- Unity 2D两种常用判断点击的方法
1.Raycast法 原理相同于3D中得Raycast法,具体使用略有区别. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorl ...
- unity 旋转两种方法
transform.Rotate(new Vector3(0, 10, 10)*speed*Time.deltaTime); // 物体绕x轴.y轴.z轴旋转 transform.RotateArou ...
- angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用
今天我们要讲的是ng2的路由系统. 例子
随机推荐
- Java网络编程Socket通信
TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议 UDP (User Datagram Proto ...
- js 异步问题
如果发现前端的异步请求,network有发送成功而且有返回值,但是没有进前端的callback成功回掉函数内,这时候就需要检查是不是配置文件做了语法限制,只能返回某些数据格式,不能返回某些数据格式. ...
- jni中c代码调用java代码
原理是使用反射的机制 java中反射的例子: Class<?> forName = Class.forName("com.example.ndkcallback.DataProv ...
- SQLite win7
https://blog.csdn.net/louislee92/article/details/50390000 vs2008利用sqlite A 添加sqlite3.h sqlite3.lib到工 ...
- C++学习笔记1-使用数组进行vector初始化
另外,如果是定义的时候,可以直接指定复制.比如:int s[5]={1,2,3,4,5};vector<int> v(s,s+5);就可以啦.
- 2.8-2.10 HBase集成MapReduce
一.HBase集成MapReduce 1.查看HBase集成MapReduce需要的jar包 [root@hadoop-senior hbase-0.98.6-hadoop2]# bin/hbase ...
- 多叉树结构:JSON数据解析(二)
多叉树结构:JSON数据解析(二) 在上篇文章中提到了JSON数据解析的基本方法,但是方法效率太低,这里接着上篇文章写写如何利用多叉树结构,定义对象,实现JSON数据字段快速随机访问. JSON数据通 ...
- Flutter实战视频-移动电商-11.首页_屏幕适配方案讲解
11.首页_屏幕适配方案讲解 国人写的屏幕适配插件: https://github.com/OpenFlutter/flutter_screenutil 最新版本是0.5.1 在pubspec.yam ...
- POJ - 3414 Pots BFS(著名倒水问题升级版)
Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...
- iOS 中使用 MJExtension 遇到 关键字(id) 怎么办
MJExtension 是个人比较喜欢用的json 转model 的软件,当遇到系统关键字时就会出现崩溃,解决方式如下 1.建立Modle 解析类,服务返回数据中带有id,这个时候用字典转Mode(m ...