SystemBase.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using SC.Tools;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public abstract class SystemBase : MonoBehaviour
  5. {
  6. internal bool isRun = true;
  7. internal PersonSystemBase parentPersonSystem;
  8. internal SystemBase parentSystem;
  9. protected List<PersonSystemBase> subPersonSystemList = new List<PersonSystemBase>();
  10. protected List<SystemBase> subSystemList = new List<SystemBase>();
  11. //public virtual void AddSubSystems() {
  12. // Debug.Log("[" + GetType().ToString() + "]: " + "AddSubSystems:");
  13. //}
  14. /// <summary>
  15. /// 子系统加入的方法
  16. /// </summary>
  17. /// <param name="subSystem"></param>
  18. public void AddSubSystem(PersonSystemBase subPersonSystem) {
  19. if (subPersonSystem == null) {
  20. Debug.Log("Error: ========= subSystem Null");
  21. return;
  22. }
  23. Debug.Log("[" + GetType().ToString() + "]: " + "AddSubSystem:" + subPersonSystem);
  24. subPersonSystemList.Add(subPersonSystem);
  25. subPersonSystem.parentSystem = this;
  26. }
  27. /// <summary>
  28. /// 子系统移除的方法
  29. /// </summary>
  30. /// <param name="subSystem"></param>
  31. public void RemoveSubSystem(PersonSystemBase subPersonSystem) {
  32. if (subPersonSystem == null) {
  33. Debug.Log("Error: ========= subSystem Null");
  34. return;
  35. }
  36. subPersonSystemList.Remove(subPersonSystem);
  37. subPersonSystem.parentSystem = null;
  38. }
  39. /// <summary>
  40. /// 子系统加入的方法
  41. /// </summary>
  42. /// <param name="subSystem"></param>
  43. public void AddSubSystem(SystemBase subSystem) {
  44. if (subSystem == null) {
  45. Debug.Log("Error: ========= subSystem Null");
  46. return;
  47. }
  48. Debug.Log("[" + GetType().ToString() + "]: " + "AddSubSystem:" + subSystem.name);
  49. subSystemList.Add(subSystem);
  50. subSystem.parentSystem = this;
  51. subSystem.gameObject.SetActive(true);
  52. subSystem.transform.SetParent(transform, false);
  53. }
  54. /// <summary>
  55. /// 子系统移除的方法
  56. /// </summary>
  57. /// <param name="subSystem"></param>
  58. public void RemoveSubSystem(SystemBase subSystem) {
  59. if (subSystem == null) {
  60. Debug.Log("Error: ========= subSystem Null");
  61. return;
  62. }
  63. subSystemList.Remove(subSystem);
  64. subSystem.parentSystem = null;
  65. DestroyImmediate(subSystem);
  66. }
  67. public virtual void SystemStart() {
  68. foreach (var item in subPersonSystemList) {
  69. item.SystemStart();
  70. }
  71. foreach (var item in subSystemList) {
  72. item.SystemStart();
  73. }
  74. isRun = true;
  75. gameObject.SetActive(true);
  76. Debug.Log("[" + GetType().ToString() + "]: " + "SystemStart");
  77. }
  78. public virtual void SystemStop() {
  79. foreach (var item in subPersonSystemList) {
  80. item.SystemStop();
  81. }
  82. foreach (var item in subSystemList) {
  83. item.SystemStop();
  84. }
  85. gameObject.SetActive(false);
  86. isRun = false;
  87. Debug.Log("[" + GetType().ToString() + "]: " + "SystemStop");
  88. }
  89. //public virtual void Action() {
  90. // if (!isRun)
  91. // return;
  92. // foreach (var item in subPersonSystemList) {
  93. // item.Action();
  94. // }
  95. // foreach (var item in subSystemList) {
  96. // item.Action();
  97. // }
  98. // Debug.Log("[" + GetType().ToString() + "]: " + "Action");
  99. //}
  100. public virtual void Awake() {
  101. if (!isRun)
  102. return;
  103. foreach (var item in subPersonSystemList) {
  104. item.Awake();
  105. }
  106. Debug.Log("[" + GetType().ToString() + "]: " + "Awake");
  107. }
  108. public virtual void OnEnable() {
  109. if (!isRun)
  110. return;
  111. foreach (var item in subPersonSystemList) {
  112. item.OnEnable();
  113. }
  114. Debug.Log("[" + GetType().ToString() + "]: " + "OnEnable");
  115. }
  116. public virtual void Start() {
  117. if (!isRun)
  118. return;
  119. foreach (var item in subPersonSystemList) {
  120. item.Start();
  121. }
  122. Debug.Log("[" + GetType().ToString() + "]: " + "Start");
  123. }
  124. public virtual void Update() {
  125. if (!isRun)
  126. return;
  127. foreach (var item in subPersonSystemList) {
  128. item.Update();
  129. }
  130. }
  131. public virtual void LateUpdate() {
  132. if (!isRun)
  133. return;
  134. foreach (var item in subPersonSystemList) {
  135. item.LateUpdate();
  136. }
  137. }
  138. public virtual void OnDisable() {
  139. if (!isRun)
  140. return;
  141. foreach (var item in subPersonSystemList) {
  142. item.OnDisable();
  143. }
  144. Debug.Log("[" + GetType().ToString() + "]: " + "OnDisable");
  145. }
  146. public virtual void OnDestroy() {
  147. if (!isRun)
  148. return;
  149. foreach (var item in subPersonSystemList) {
  150. item.OnDestroy();
  151. }
  152. Debug.Log("[" + GetType().ToString() + "]: " + "OnDestroy");
  153. }
  154. }