SingleTon.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using UnityEngine;
  10. namespace NRKernal
  11. {
  12. /// <summary> A single ton. </summary>
  13. /// <typeparam name="T"> Generic type parameter.</typeparam>
  14. public class SingleTon<T> where T : new()
  15. {
  16. private static T instane = default(T);
  17. public static void CreateInstance()
  18. {
  19. if(instane == null)
  20. {
  21. instane = new T();
  22. }
  23. }
  24. /// <summary> Gets the instance. </summary>
  25. /// <value> The instance. </value>
  26. public static T Instance
  27. {
  28. get
  29. {
  30. if(instane == null)
  31. {
  32. CreateInstance();
  33. }
  34. return instane;
  35. }
  36. }
  37. }
  38. /// <summary> A singleton behaviour. </summary>
  39. /// <typeparam name="T"> Generic type parameter.</typeparam>
  40. public class SingletonBehaviour<T> : MonoBehaviour where T : SingletonBehaviour<T>
  41. {
  42. /// <summary> The instance. </summary>
  43. private static T instance;
  44. /// <summary>
  45. /// Returns the Singleton instance of the classes type. If no instance is found, then we search
  46. /// for an instance in the scene. If more than one instance is found, we throw an error and no
  47. /// instance is returned. </summary>
  48. /// <value> The instance. </value>
  49. protected static T Instance
  50. {
  51. get
  52. {
  53. if (instance == null && searchForInstance)
  54. {
  55. searchForInstance = false;
  56. T[] objects = FindObjectsOfType<T>();
  57. if (objects.Length == 1)
  58. {
  59. instance = objects[0];
  60. }
  61. else if (objects.Length > 1)
  62. {
  63. Debug.LogErrorFormat("Expected exactly 1 {0} but found {1}.", typeof(T).ToString(), objects.Length);
  64. }
  65. }
  66. return instance;
  67. }
  68. }
  69. /// <summary> True to search for instance. </summary>
  70. private static bool searchForInstance = true;
  71. /// <summary> Assert is initialized. </summary>
  72. public static void AssertIsInitialized()
  73. {
  74. Debug.Assert(IsInitialized, string.Format("The {0} singleton has not been initialized.", typeof(T).Name));
  75. }
  76. /// <summary> Returns whether the instance has been initialized or not. </summary>
  77. /// <value> True if this object is initialized, false if not. </value>
  78. public static bool IsInitialized
  79. {
  80. get
  81. {
  82. return instance != null;
  83. }
  84. }
  85. /// <summary> True if is dirty, false if not. </summary>
  86. protected bool isDirty = false;
  87. /// <summary>
  88. /// Base Awake method that sets the Singleton's unique instance. Called by Unity when
  89. /// initializing a MonoBehaviour. Scripts that extend Singleton should be sure to call
  90. /// base.Awake() to ensure the static Instance reference is properly created. </summary>
  91. protected virtual void Awake()
  92. {
  93. if (IsInitialized && instance != this)
  94. {
  95. isDirty = true;
  96. DestroyImmediate(gameObject);
  97. NRDebugger.Info("Trying to instantiate a second instance of singleton class {0}. Additional Instance was destroyed", GetType().Name);
  98. }
  99. else if (!IsInitialized)
  100. {
  101. instance = (T)this;
  102. DontDestroyOnLoad(gameObject);
  103. }
  104. }
  105. /// <summary>
  106. /// Base OnDestroy method that destroys the Singleton's unique instance. Called by Unity when
  107. /// destroying a MonoBehaviour. Scripts that extend Singleton should be sure to call
  108. /// base.OnDestroy() to ensure the underlying static Instance reference is properly cleaned up. </summary>
  109. protected virtual void OnDestroy()
  110. {
  111. if (instance == this)
  112. {
  113. instance = null;
  114. searchForInstance = true;
  115. }
  116. }
  117. }
  118. }