Unity3D的单例模式实现

来源:互联网 发布:淘宝网毛呢长裙 编辑:程序博客网 时间:2024/06/07 15:33


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;
    }

0 0
原创粉丝点击