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

카테고리

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

'MonoSingleton'에 해당되는 글 1건

  1. 2014.03.24 Unity Singleton

Unity Singleton

Unity3D/Script / 2014. 3. 24. 18:36
  1. using UnityEngine;  
  2.   
  3. public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>  
  4. {  
  5.     protected static bool m_bDontDestroyOnLoad = true;  
  6.       
  7.     private static bool m_bApplicationQuit = false;  
  8.     private static object InstanceLocker = new object();  
  9.   
  10.     private static T m_Instance = null;  
  11.     public static T Instance  
  12.     {  
  13.         get  
  14.         {  
  15.             if (true == m_bApplicationQuit)  
  16.                 return null;  
  17.   
  18.             lock (InstanceLocker)  
  19.             {  
  20.                 // Instance requiered for the first time, we look for it  
  21.                 if (null == m_Instance)  
  22.                 {  
  23.                     T instance = GameObject.FindObjectOfType(typeof(T)) as T;  
  24.   
  25.                     // Object not found, we create a temporary one  
  26.                     if (instance == null)  
  27.                     {  
  28.                         instance = new GameObject(typeof(T).ToString()).AddComponent<T>();  
  29.   
  30.                         // Problem during the creation, this should not happen  
  31.                         if (instance == null)  
  32.                         {  
  33.                             Debug.LogError("Problem during the creation of " + typeof(T).ToString());  
  34.                         }  
  35.                     }  
  36.   
  37.                     if (instance != null)  
  38.                     {  
  39.                         Initialize(instance);  
  40.                     }  
  41.                 }  
  42.   
  43.                 return m_Instance;  
  44.             }  
  45.         }  
  46.     }  
  47.   
  48.     private static void Initialize(T instance)  
  49.     {  
  50.         if (m_Instance == null)  
  51.         {  
  52.             var startTime = System.DateTime.Now;  
  53.             m_Instance = instance;  
  54.   
  55.             // 씬 전환 시, 삭제시킬 싱글톤은 부모 객체에 안붙이도록..  
  56.             // 싱글톤 시작 시, m_bDontDestroyOnLoad 셋팅 필요.  
  57.             if (true == m_bDontDestroyOnLoad)  
  58.             {  
  59.                 GameObject goRoot = GameObject.Find("Singletons"as GameObject;  
  60.                 if (null == goRoot)  
  61.                 {  
  62.                     goRoot = new GameObject("Singletons");  
  63.                     // DontDestroyOnLoad() 등록은 하위 상속받는 쪽에서 하도록 하는 게 나을까?  
  64.                     DontDestroyOnLoad(goRoot);  
  65.                 }  
  66.                 m_Instance.transform.parent = goRoot.transform;  
  67.             }  
  68.   
  69.             m_Instance.OnInitialize();  
  70.             var period = System.DateTime.Now - startTime;  
  71.             if (period.TotalSeconds > 1.0f)  
  72.             {  
  73.                 var name = m_Instance.ToString();  
  74.                 Debug.LogWarning("Profile Warnning. Singletion {" + name + "} too long init time : "   
  75.                                  + period.TotalSeconds.ToString("F") + "Seconds");  
  76.             }  
  77.         }  
  78.         else if (m_Instance != instance)  
  79.         {  
  80.             DestroyImmediate(instance.gameObject);  
  81.         }  
  82.     }  
  83.   
  84.     private static void Destroyed(T instance)  
  85.     {  
  86.         if (m_Instance == instance)  
  87.         {  
  88.             m_Instance.OnFinalize();  
  89.             m_Instance = null;  
  90.         }  
  91.     }  
  92.   
  93.     public void CreateSingleton() { }  
  94.     // [Warning] GameObject에 Component로 미리 등록된 상태에서는 OnInitialize() 호출 안됨.  
  95.     public virtual void OnInitialize() { }  
  96.     // [Warning] GameObject에 Component로 미리 등록된 상태에서는 OnFinalize() 호출 안됨.  
  97.     public virtual void OnFinalize() { }  
  98.     protected virtual void CheckDontDestroyOnLoad() { }  
  99.   
  100.     private void Awake()  
  101.     {  
  102.         Initialize(this as T);  
  103.     }  
  104.   
  105.     void OnDestroy()  
  106.     {  
  107.         Destroyed(this as T);  
  108.     }  
  109.   
  110.     private void OnApplicationQuit()  
  111.     {  
  112.         m_bApplicationQuit = true;  
  113.         Destroyed(this as T);  
  114.     }  
  115. }  


반응형
Posted by blueasa
, |