Singleton.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace socket
  5. {
  6. //public class Singleton<T> where T : new()
  7. //{
  8. // static protected T sInstance;
  9. // static protected bool IsCreate = false;
  10. // public static T Instance
  11. // {
  12. // get
  13. // {
  14. // if (IsCreate == false)
  15. // {
  16. // CreateInstance();
  17. // }
  18. // return sInstance;
  19. // }
  20. // }
  21. // public static void CreateInstance()
  22. // {
  23. // if (IsCreate == true)
  24. // return;
  25. // IsCreate = true;
  26. // sInstance = new T();
  27. // }
  28. // public static void ReleaseInstance()
  29. // {
  30. // sInstance = default(T);
  31. // IsCreate = false;
  32. // }
  33. //}
  34. //public abstract class SingletonMonoBehaviour<T> : MonoBehaviour where T : SingletonMonoBehaviour<T>
  35. //{
  36. // protected static T sInstance = null;
  37. // protected static bool IsCreate = false;
  38. // public static bool s_debugDestroy = false;
  39. // public static T Instance
  40. // {
  41. // get
  42. // {
  43. // if (s_debugDestroy)
  44. // {
  45. // return null;
  46. // }
  47. // CreateInstance();
  48. // return sInstance;
  49. // }
  50. // }
  51. // protected virtual void Awake()
  52. // {
  53. // if (sInstance == null)
  54. // {
  55. // sInstance = this as T;
  56. // IsCreate = true;
  57. // Init();
  58. // }
  59. // }
  60. // protected virtual void Init()
  61. // {
  62. // }
  63. // protected virtual void OnDestroy()
  64. // {
  65. // sInstance = null;
  66. // IsCreate = false;
  67. // }
  68. // private void OnApplicationQuit()
  69. // {
  70. // sInstance = null;
  71. // IsCreate = false;
  72. // }
  73. // public static void CreateInstance()
  74. // {
  75. // if (IsCreate == true)
  76. // return;
  77. // IsCreate = true;
  78. // T[] managers = GameObject.FindObjectsOfType(typeof(T)) as T[];
  79. // if (managers.Length != 0)
  80. // {
  81. // if (managers.Length == 1)
  82. // {
  83. // sInstance = managers[0];
  84. // sInstance.gameObject.name = typeof(T).Name;
  85. // DontDestroyOnLoad(sInstance.gameObject);
  86. // return;
  87. // }
  88. // else
  89. // {
  90. // foreach (T manager in managers)
  91. // {
  92. // Destroy(manager.gameObject);
  93. // }
  94. // }
  95. // }
  96. // GameObject gO = new GameObject(typeof(T).Name, typeof(T));
  97. // sInstance = gO.GetComponent<T>();
  98. // DontDestroyOnLoad(sInstance.gameObject);
  99. // }
  100. // public static void ReleaseInstance()
  101. // {
  102. // if (sInstance != null)
  103. // {
  104. // Destroy(sInstance.gameObject);
  105. // sInstance = null;
  106. // IsCreate = false;
  107. // }
  108. // }
  109. //}
  110. }