Unity3D的单例模式实现
2017-03-13 17:12
253 查看
using UnityEngine;
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>{
private static T m_Instance = null;
public static T instance{
get{
if( m_Instance == null ){
m_Instance = GameObject.FindObjectOfType(typeof(T)) as T;
if( m_Instance == null ){
m_Instance = new GameObject("Singleton of " + typeof(T).ToString(), typeof(T)).GetComponent<T>();
m_Instance.Init();
}
}
return m_Instance;
}
}
private void Awake(){
if( m_Instance == null ){
m_Instance = this as T;
}
}
public virtual void Init(){}
private void OnApplicationQuit(){
m_Instance = null;
}
using UnityEngine;
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>{
private static T m_Instance = null;
public static T instance{
get{
if( m_Instance == null ){
m_Instance = GameObject.FindObjectOfType(typeof(T)) as T;
if( m_Instance == null ){
m_Instance = new GameObject("Singleton of " + typeof(T).ToString(), typeof(T)).GetComponent<T>();
m_Instance.Init();
}
}
return m_Instance;
}
}
private void Awake(){
if( m_Instance == null ){
m_Instance = this as T;
}
}
public virtual void Init(){}
private void OnApplicationQuit(){
m_Instance = null;
}
相关文章推荐
- Unity3D中实现简单的电影模式框架
- Unity3D中实现简单的电影模式框架
- 在Unity3D中基于订阅者模式实现事件机制
- Unity3d基于订阅者模式实现事件机制_解决装箱拆箱问题和注册的监听事件单一问题
- 在Unity3D中基于订阅者模式怎样实现事件机制
- Unity3d 单例模式的实现
- 游戏开发设计模式之子类沙盒模式(unity3d 示例实现)
- 游戏开发设计模式之状态模式 & 有限状态机 & c#委托事件(unity3d 示例实现)
- Unity3D之利用C#实现代理模式
- 游戏开发设计模式之命令模式(unity3d 示例实现)
- 游戏开发设计模式之命令模式(unity3d 示例实现)
- 游戏开发设计模式之子类沙盒模式(unity3d 示例实现)
- 游戏开发设计模式之原型模式 & unity3d JSON的使用(unity3d 示例实现)
- Unity3D的单例模式实现
- Unity3d C# 脚本单体模式的实现
- 游戏开发设计模式之对象池模式(unity3d 示例实现)
- Unity3D 利用C#实现简单的代理模式Delegate(委托)
- 游戏开发设计模式之状态模式 & 有限状态机 & c#委托事件(unity3d 示例实现)
- Unity教程之-Unity3d中基于订阅者模式实现事件机制
- 在Unity3d中实现观察者模式