Singleton.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using UnityEngine;
  2. using System.Collections;
  3. public abstract class Singleton<T> where T : new()
  4. {
  5. private static T _instance;
  6. static object _lock = new object();
  7. public static T Instance
  8. {
  9. get
  10. {
  11. if (_instance == null)
  12. {
  13. lock (_lock)
  14. {
  15. if (_instance == null)
  16. _instance = new T();
  17. }
  18. }
  19. return _instance;
  20. }
  21. }
  22. }
  23. /// <summary>
  24. /// Be aware this will not prevent a non singleton constructor5.
  25. /// such as `T myT = new T();`
  26. /// To prevent that, add `protected T () {}` to your singleton class.
  27. /// </summary>
  28. public class UnitySingleton<T> : MonoBehaviour where T : MonoBehaviour
  29. {
  30. private static T _instance;
  31. private static object _lock = new object ();
  32. public static T Instance {
  33. get {
  34. lock (_lock) {
  35. if (applicationIsQuitting) {
  36. return null;
  37. }
  38. if (_instance == null) {
  39. _instance = (T)FindObjectOfType (typeof(T));
  40. if (_instance == null) {
  41. string goName = typeof(T).ToString ();
  42. GameObject go = GameObject.Find (goName);
  43. if (go == null) {
  44. go = new GameObject ();
  45. _instance = go.AddComponent<T> ();
  46. go.name = "(singleton) " + typeof(T).ToString ();
  47. //go.hideFlags = HideFlags.HideAndDontSave;
  48. DontDestroyOnLoad (go);
  49. }
  50. } else {
  51. }
  52. }
  53. return _instance;
  54. }
  55. }
  56. }
  57. private static bool applicationIsQuitting = false;
  58. // private static bool ClearMode = false;
  59. public static void DestorySingleton ()
  60. {
  61. lock (_lock) {
  62. applicationIsQuitting = true;
  63. if (_instance != null) {
  64. // ClearMode = true;
  65. GameObject.Destroy (_instance.gameObject);
  66. _instance = null;
  67. }
  68. }
  69. }
  70. // public void OnDestroy() {
  71. // //Debug.LogError ("unitySingle OnDestroy");
  72. // lock (_lock) {
  73. // applicationIsQuitting = true;
  74. // if (_instance != null) {
  75. // GameObject.Destroy (_instance.gameObject);
  76. // _instance = null;
  77. // }
  78. // }
  79. // }
  80. }
  81. public class UnitySingletonLoadDestory<T> : MonoBehaviour where T : MonoBehaviour
  82. {
  83. private static T _instance;
  84. private static object _lock = new object ();
  85. public static T Instance {
  86. get {
  87. if (applicationIsQuitting) {
  88. return null;
  89. }
  90. lock (_lock) {
  91. if (_instance == null) {
  92. _instance = (T)FindObjectOfType (typeof(T));
  93. if (_instance == null) {
  94. string goName = typeof(T).ToString ();
  95. GameObject go = GameObject.Find (goName);
  96. if (go == null) {
  97. go = new GameObject ();
  98. _instance = go.AddComponent<T> ();
  99. go.name = "(singleton) " + typeof(T).ToString ();
  100. //go.hideFlags = HideFlags.HideAndDontSave;
  101. go.hideFlags = HideFlags.DontSave;
  102. }
  103. } else {
  104. }
  105. }
  106. return _instance;
  107. }
  108. }
  109. }
  110. private static bool applicationIsQuitting = false;
  111. public static void DestorySingleton ()
  112. {
  113. lock (_lock) {
  114. applicationIsQuitting = true;
  115. if (_instance != null) {
  116. GameObject.Destroy (_instance.gameObject);
  117. _instance = null;
  118. }
  119. }
  120. }
  121. public void OnDestroy()
  122. {
  123. lock (_lock) {
  124. applicationIsQuitting = true;
  125. if (_instance != null) {
  126. GameObject.Destroy (_instance.gameObject);
  127. _instance = null;
  128. }
  129. }
  130. }
  131. }