블로그 이미지
Every unexpected event is a path to learning for you. blueasa

카테고리

분류 전체보기 (2879)
Unity3D (904)
Programming (479)
AI (1)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (198)
협업 (65)
3DS Max (3)
Game (12)
Utility (144)
Etc (100)
Link (34)
Portfolio (19)
Subject (90)
iOS,OSX (56)
Android (16)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (4)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (21)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday

[파일]

MonoSingleton.cs
0.00MB

 

 

using UnityEngine;

namespace Core
{
    public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
    {
        private static bool m_bApplicationQuit = false;
        private static object InstanceLocker = new object();

        private static T m_Instance = null;
        public static T Instance
        {
            get
            {
                if (true == m_bApplicationQuit)
                    return null;

                lock (InstanceLocker)
                {
                    if (null == m_Instance)
                    {
                        T instance = FindAnyObjectByType<T>();

                        if (null == instance)
                        {
                            instance = new GameObject(typeof(T).ToString()).AddComponent<T>();

                            if (null == instance)
                            {
                                Debug.LogError("Problem during the creation of " + typeof(T).ToString());
                            }
                        }

                        if (null != instance)
                        {
                            Initialize(instance);
                        }
                    }

                    return m_Instance;
                }
            }
        }

        public static void SetInstance(T _cInstance)
        {
            m_Instance = _cInstance;
        }

        public static bool Exists
        {
            get
            {
                if (null == m_Instance || null == m_Instance.gameObject)
                {
                    return false;
                }
                return true;
            }
        }

        protected virtual void Awake()
        {
            Initialize(this as T);
        }

        private static void Initialize(T instance)
        {
            if (null == m_Instance)
            {
#if UNITY_EDITOR || DEVELOPMENT_BUILD
                var stopwatch = System.Diagnostics.Stopwatch.StartNew();
#endif
                m_Instance = instance;
                m_Instance.OnInitialize();

#if UNITY_EDITOR || DEVELOPMENT_BUILD
                stopwatch.Stop();
                if (1.0 < stopwatch.Elapsed.TotalSeconds)
                {
                    Debug.LogWarning($"[MonoSingleton] {typeof(T).Name} init: {stopwatch.Elapsed.TotalSeconds:F2}s");
                }
#endif
            }
            else if (m_Instance != instance)
            {
                if (true == Application.isPlaying)
                {
                    Destroy(instance.gameObject);
                }
                else
                {
                    DestroyImmediate(instance.gameObject);
                }
            }
        }

        private static void Destroyed(T instance)
        {
            if (null != m_Instance && m_Instance == instance)
            {
                m_Instance.OnFinalize();
                m_Instance = null;
            }
        }

        public void CreateSingleton() { }
        public virtual void OnInitialize() { }
        public virtual void OnFinalize() { }
        public virtual void DoDestroy() { OnDestroy(); }

        void OnDestroy()
        {
            Destroyed(this as T);
        }

        private void OnApplicationQuit()
        {
            m_bApplicationQuit = true;
            Destroyed(this as T);
        }

        static Transform GetParent(Transform _trTarget)
        {
            if (null != _trTarget.parent)
                return GetParent(_trTarget.parent);

            return _trTarget;
        }

        public void SetDontDestroy()
        {
            if (null == m_Instance)
                return;

            Transform trRoot = GetParent(this.transform);
            if (true == Application.isPlaying)
            {
                DontDestroyOnLoad(trRoot);
            }
        }
    }
}

 

 
 
반응형
Posted by blueasa
, |