demonixis

Singleton

Sep 11th, 2014
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 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.                 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.                         _instance = singleton.AddComponent<T>();
  45.                         singleton.name = "(singleton) "+ typeof(T).ToString();
  46.  
  47.                         DontDestroyOnLoad(singleton);
  48.  
  49.                         Debug.Log("[Singleton] An instance of " + typeof(T) +
  50.                             " is needed in the scene, so '" + singleton +
  51.                             "' was created with DontDestroyOnLoad.");
  52.                     } else {
  53.                         Debug.Log("[Singleton] Using instance already created: " +
  54.                             _instance.gameObject.name);
  55.                     }
  56.                 }
  57.  
  58.                 return _instance;
  59.             }
  60.         }
  61.     }
  62.  
  63.     private static bool applicationIsQuitting = false;
  64.     /// <summary>
  65.     /// When Unity quits, it destroys objects in a random order.
  66.     /// In principle, a Singleton is only destroyed when application quits.
  67.     /// If any script calls Instance after it have been destroyed,
  68.     ///   it will create a buggy ghost object that will stay on the Editor scene
  69.     ///   even after stopping playing the Application. Really bad!
  70.     /// So, this was made to be sure we're not creating that buggy ghost object.
  71.     /// </summary>
  72.     public void OnDestroy () {
  73.         applicationIsQuitting = true;
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment