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. 我在面试.NET/C#程序员时会提出的问题

    我在面试.NET/C#程序员时会提出的问题 2011-03-03 15:38 by 老赵, 28107 visits 说起来我也面试过相当数量的.NET(包括C#,后文不重复)程序员了,有的通过电话, ...

  2. Power Strings--KMP

    https://cn.vjudge.net/problem/POJ-2406 上面是比赛链接. 题目意思很明确,问最多是多少个子串连接而成的? 这个需要用到KMP,很好的理解KMP的Next数组.Ne ...

  3. html5 WebWorkers 防止浏览器假死

    在Web开发的时候经常会遇到浏览器不响应事件进入假死状态,甚至弹出“脚本运行时间过长“的提示框,如果出现这种情况说明你的脚本已经失控了. 一个浏览器至少存在三个线程:js引擎线程(处理js).GUI渲 ...

  4. C# 使用WebClient时,在多网卡时,指定IP发送Web请求

    需要定义一个类,重写GetWebRequest,在方法内,指定IP地址 public class MyWebClient : WebClient { private IPAddress ipAddre ...

  5. hue集成各种组件

    一.Hue安装 可以编译安装,我这里有已经编译好的,直接解压使用: hue默认端口:8888 http://gethue.com/ https://github.com/cloudera/hue ht ...

  6. Flutter实战视频-移动电商-27.列表页_现有Bug修复和完善

    27.列表页_现有Bug修复和完善 小解决小bug 默认右侧的小类没有被加载 数据加载完成后,就list的第一个子对象传递给provide进行赋值,这样右侧的小类就刷新了数据 默认加载了第一个类别 调 ...

  7. 51nod1010【二分】

    打表+二分 #include <bits/stdc++.h> using namespace std; typedef long long LL; const LL inf=1e18+10 ...

  8. VC++11 编译中的一些问题的解决办法

    1.  vc++ 的编译器的错误往往定位在错误的那一处,但是那一处可能在库的底层,而我们知道库,一般都不会错. 这时候应该好好看看我们自己的头文件是否正确,有可能头文件中的一些错误引发了连锁反应. 2 ...

  9. hyperledger fabric 1.0.5 分布式部署 (六)

    如何在相同的peer 节点上创建多个 channel 作者在hyperledger fabric 1.0.5 分布式部署 (五)已经向读者们介绍了一个简单的fabric 的部署流程,那么根据上一篇博客 ...

  10. scrapy 安装错误

    真的是各种坑啊,哎 安装显示 Building wheel for twisted (setup.py) ... error 解决方法: https://askubuntu.com/questions ...