CreateSingleton.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using UnityEngine;
  5. namespace MagicaCloth
  6. {
  7. /// <summary>
  8. /// 基本的なシングルトンテンプレート
  9. /// ・シーンに無い場合は作成する
  10. /// ・自動初期化呼び出し機能
  11. /// ・DontDestroyOnLoad設定
  12. /// ・実行前でもInstanceアクセス可能
  13. /// </summary>
  14. /// <typeparam name="T"></typeparam>
  15. public abstract class CreateSingleton<T> : MonoBehaviour where T : MonoBehaviour
  16. {
  17. private static T instance;
  18. /// <summary>
  19. /// 初期化フラグ
  20. /// </summary>
  21. private static T initInstance;
  22. private static bool isDestroy;
  23. /// <summary>
  24. /// Reload Domain 対応
  25. /// ※残念ながらジェネリッククラスでは[RuntimeInitializeOnLoadMethod]が利用できないため、
  26. /// この初期化関数を派生元で[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  27. /// を使用して呼び出さなければならない
  28. /// </summary>
  29. protected static void InitMember()
  30. {
  31. instance = null;
  32. initInstance = null;
  33. isDestroy = false;
  34. }
  35. public static T Instance
  36. {
  37. get
  38. {
  39. if (instance == null)
  40. {
  41. // FindObjectOfTypeはそれなりに負荷がかかるので注意!
  42. // 非アクティブのオブジェクトは発見できないので注意!
  43. instance = FindObjectOfType<T>();
  44. if (instance == null && Application.isPlaying)
  45. {
  46. var obj = new GameObject(typeof(T).Name);
  47. instance = obj.AddComponent<T>();
  48. }
  49. }
  50. // 初期化
  51. InitInstance();
  52. return instance;
  53. }
  54. }
  55. private static void InitInstance()
  56. {
  57. if (initInstance == null && instance != null && Application.isPlaying)
  58. {
  59. // シーン切り替えでもオブジェクトが消えないように設定
  60. DontDestroyOnLoad(instance.gameObject);
  61. // 初期化呼び出し
  62. var s = instance as CreateSingleton<T>;
  63. s.InitSingleton();
  64. initInstance = instance;
  65. }
  66. }
  67. /// <summary>
  68. /// インスタンスが存在する場合にTrueを返します
  69. /// </summary>
  70. /// <returns></returns>
  71. public static bool IsInstance()
  72. {
  73. return instance != null && isDestroy == false;
  74. }
  75. /// <summary>
  76. /// Awake()でのインスタンス設定
  77. /// </summary>
  78. protected virtual void Awake()
  79. {
  80. if (instance == null)
  81. {
  82. instance = this as T;
  83. InitInstance();
  84. }
  85. else if(instance != this)
  86. {
  87. // 2つ目のコンポーネントを発見
  88. var s = instance as CreateSingleton<T>;
  89. s.DuplicateDetection(this as T);
  90. // 2つ目のコンポーネントは破棄する
  91. Destroy(this.gameObject);
  92. }
  93. }
  94. protected virtual void OnDestroy()
  95. {
  96. // インスタンスクラスならば無効化フラグを立てる
  97. if (instance == this)
  98. {
  99. isDestroy = true;
  100. }
  101. }
  102. /// <summary>
  103. /// 2つ目の破棄されるコンポーネントを通知
  104. /// </summary>
  105. /// <param name="duplicate"></param>
  106. protected virtual void DuplicateDetection(T duplicate) { }
  107. /// <summary>
  108. /// 内部初期化
  109. /// </summary>
  110. protected abstract void InitSingleton();
  111. }
  112. }