using UnityEngine;
///
/// 单例模式
///
///
public abstract class MonoSingleton : MonoBehaviour where T : MonoSingleton{
private static T m_Instance = null;
//设计阶段 脚本没有挂在物体上
//运行时 需要这个脚本的唯一实例,调用instance
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(typeof(T).ToString(), typeof(T)).GetComponent();
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;
}
}