반응형
Notice
Recent Posts
Recent Comments
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Do Something IT

MonoSingleton 본문

Unity3D

MonoSingleton

아낙시만더 2015. 9. 8. 11:09
반응형
using UnityEngine;

public abstract class MonoSingleton : MonoBehaviour where T : MonoSingleton
{
    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)
				{
					//Debug.LogError("No instance of " + typeof(T).ToString());
					Debug.Log("No instance of " + typeof(T).ToString());
				}
				else
				{
					m_Instance.Init();
				}
            }
            return m_Instance;
        }
    }

    private void Awake()
    {
        if (m_Instance == null)
        {
            m_Instance = this as T;

            m_Instance.Init();
        }
    }

    public virtual void Init() { } // 초기화를 상속을 통해 구현

    private void OnApplicationQuit()
    {
        m_Instance = null;
    }

    void OnEnable()
    {
        m_Instance = null;
    }
}

반응형

'Unity3D' 카테고리의 다른 글

라이브2D  (0) 2016.02.04
fps  (0) 2015.09.24
lens flare 햇빛 효과  (0) 2015.09.04
비주얼 스튜디오 편한 단축키  (0) 2015.08.21
유니티 모바일 터치 Tutorial  (0) 2015.07.22
Comments