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. html5--5-3 给直线添加样式

    html5--5-3 给直线添加样式 学习要点 strokeStyle属性:设置颜色.渐变或模式(本节课只涉及到颜色) lineWidth属性:--设置线宽 Canvas的路径方法 moveTo() ...

  2. nyoj 1030 hihocoder 1338

    题目链接1: 点这里打开. 题目链接2:   点击打开链接 思路:dp,dp[i][j] 表示某个人在区间 i,j上的得分. sum数组表示前 n 项和, num 数组用来存输入的数字. 因为取数字是 ...

  3. No result defined for action cn.crm.action.LinkManAction and result input

    这是struts2的一个拦截器报的错误,当你的form中的数据有问题,比如说<input type="text" name="receiverLoginID&quo ...

  4. Java应用架构设计模块化模式与OSGI摘录

    在Java中,最适合模块化的单元就是Jar文件. 代码层面我们关注的太多了,熟练的开发人员现在很少争论使用模式的好处,也不再识别哪个模式适合当前需要,因为都能够本能地使用各种设计原则和模式,从GoF的 ...

  5. bzoj 2962 序列操作——线段树(卷积?)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2962 如果 _,_,_,…… 变成了 (_+k),(_+k),(_+k),…… ,计算就是在 ...

  6. JAVA解析EXCEL(2003和2007)

    本文参考: http://wenku.baidu.com/view/707f07d95022aaea998f0fd1.html http://surfingforrest.iteye.com/blog ...

  7. JAVA 内部类 (一)

    可将一个类定义置入另一个类定义中.这就叫作“内部类”.内部类对我们非常有用,因为利用它可对那些逻辑上相互联系的类进行分组,并可控制一个类在另一个类里的“可见性”.然而,我们必须认识到内部类与以前讲述的 ...

  8. 修改SQL Server 2005的默认端口

    修改SQL Server 2005的默认端口 1.打开SQL Server的配置管理程序 Microsoft SQL Server 2005->配置工具->SQL Server Confi ...

  9. codevs-1205

    1205 单词翻转 题目描述 Description 给出一个英语句子,希望你把句子里的单词顺序都翻转过来 输入描述 Input Description 输入包括一个英语句子. 输出描述 Output ...

  10. Quartz实现定期运行程序(Java)

    package Quartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; imp ...