Unity3D单例类模板类

C#代码  收藏代码
  1. using UnityEngine;  
  2.   
  3. /// <summary>  
  4. /// Be aware this will not prevent a non singleton constructor  
  5. ///   such as `T myT = new T();`  
  6. /// To prevent that, add `protected T () {}` to your singleton class.  
  7. ///   
  8. /// As a note, this is made as MonoBehaviour because we need Coroutines.  
  9. /// </summary>  
  10. public class Singleton<T> : MonoBehaviour where T : MonoBehaviour  
  11. {  
  12.     private static T _instance;  
  13.       
  14.     private static object _lock = new object();  
  15.       
  16.     public static T Instance  
  17.     {  
  18.         get  
  19.         {  
  20.             if (applicationIsQuitting) {  
  21.                 Debug.LogWarning("[Singleton] Instance '"+ typeof(T) +  
  22.                                  "' already destroyed on application quit." +  
  23.                                  " Won't create again - returning null.");  
  24.                 return null;  
  25.             }  
  26.               
  27.             lock(_lock)  
  28.             {  
  29.                 if (_instance == null)  
  30.                 {  
  31.                     _instance = (T) FindObjectOfType(typeof(T));  
  32.                       
  33.                     if ( FindObjectsOfType(typeof(T)).Length > 1 )  
  34.                     {  
  35.                         Debug.LogError("[Singleton] Something went really wrong " +  
  36.                                        " - there should never be more than 1 singleton!" +  
  37.                                        " Reopenning the scene might fix it.");  
  38.                         return _instance;  
  39.                     }  
  40.                       
  41.                     if (_instance == null)  
  42.                     {  
  43.                         GameObject singleton = new GameObject();  
  44.   
  45.   
  46.                         _instance = singleton.AddComponent<T>();  
  47.                         singleton.name = "(singleton) "+ typeof(T).ToString();  
  48.                           
  49.                         DontDestroyOnLoad(singleton);  
  50.                           
  51.                         Debug.Log("[Singleton] An instance of " + typeof(T) +   
  52.                                   " is needed in the scene, so '" + singleton +  
  53.                                   "' was created with DontDestroyOnLoad.");  
  54.                     } else {  
  55.                         Debug.Log("[Singleton] Using instance already created: " +  
  56.                                   _instance.gameObject.name);  
  57.                     }  
  58.                 }  
  59.                   
  60.                 return _instance;  
  61.             }  
  62.         }  
  63.     }  
  64.       
  65.     private static bool applicationIsQuitting = false;  
  66.     /// <summary>  
  67.     /// When Unity quits, it destroys objects in a random order.  
  68.     /// In principle, a Singleton is only destroyed when application quits.  
  69.     /// If any script calls Instance after it have been destroyed,   
  70.     ///   it will create a buggy ghost object that will stay on the Editor scene  
  71.     ///   even after stopping playing the Application. Really bad!  
  72.     /// So, this was made to be sure we're not creating that buggy ghost object.  
  73.     /// </summary>  
  74.     public void OnDestroy () {  
  75.         applicationIsQuitting = true;  
  76.     }  
  77. }  

 小结:

原文地址:https://www.cnblogs.com/harlan1009/p/4282319.html