Edwarddv

Singleton

Jul 20th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  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.             {
  22.                 Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
  23.                     "' already destroyed on application quit." +
  24.                     " Won't create again - returning null.");
  25.                 return null;
  26.             }
  27.  
  28.             lock (_lock)
  29.             {
  30.                 if (_instance == null)
  31.                 {
  32.                     _instance = (T)FindObjectOfType(typeof(T));
  33.  
  34.                     if (FindObjectsOfType(typeof(T)).Length > 1)
  35.                     {
  36.                         Debug.LogError("[Singleton] Something went really wrong " +
  37.                             " - there should never be more than 1 singleton!" +
  38.                             " Reopening the scene might fix it.");
  39.                         return _instance;
  40.                     }
  41.  
  42.                     if (_instance == null)
  43.                     {
  44.                         GameObject singleton = new GameObject();
  45.                         _instance = singleton.AddComponent<T>();
  46.                         singleton.name = "(singleton) " + typeof(T).ToString();
  47.  
  48.                         DontDestroyOnLoad(singleton);
  49.  
  50.                         Debug.Log("[Singleton] An instance of " + typeof(T) +
  51.                             " is needed in the scene, so '" + singleton +
  52.                             "' was created with DontDestroyOnLoad.");
  53.                     }
  54.                     else
  55.                     {
  56.                         Debug.Log("[Singleton] Using instance already created: " +
  57.                             _instance.gameObject.name);
  58.                     }
  59.                 }
  60.  
  61.                 return _instance;
  62.             }
  63.         }
  64.     }
  65.  
  66.     private static bool applicationIsQuitting = false;
  67.     /// <summary>
  68.     /// When Unity quits, it destroys objects in a random order.
  69.     /// In principle, a Singleton is only destroyed when application quits.
  70.     /// If any script calls Instance after it have been destroyed,
  71.     ///   it will create a buggy ghost object that will stay on the Editor scene
  72.     ///   even after stopping playing the Application. Really bad!
  73.     /// So, this was made to be sure we're not creating that buggy ghost object.
  74.     /// </summary>
  75.     public void OnDestroy()
  76.     {
  77.         applicationIsQuitting = true;
  78.     }
  79. }
Add Comment
Please, Sign In to add comment