SCModuleMono.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC.XR.Unity {
  5. public abstract class SCModuleMono : MonoBehaviour, ISCModule {
  6. public string ModuleName { get; set; }
  7. public bool IsModuleInit { get; set; }
  8. public bool IsModuleStarted { get; set; }
  9. public ISCModule FatherModule { get; set; }
  10. List<ISCModule> subModuleList = new List<ISCModule>();
  11. public bool IsMono { get; set; }
  12. public SCModulePriority Priority { get; set; }
  13. public bool IsEffectGameObject { get; set; }
  14. /// <summary>
  15. /// Module 初始化 ===================================
  16. /// </summary>
  17. public void ModuleInit(bool isEffectGameObject = true, SCModulePriority priority = SCModulePriority.Middle) {
  18. if(IsModuleInit) {
  19. DebugMy.LogError("ModuleInit Had Invoke", this);
  20. return;
  21. }
  22. IsModuleInit = true;
  23. ModuleName = GetType().ToString();
  24. IsMono = true;
  25. IsEffectGameObject = isEffectGameObject;
  26. Priority = priority;
  27. OnSCAwake();
  28. if(IsEffectGameObject) {
  29. gameObject.SetActive(false);
  30. }
  31. }
  32. public virtual void OnSCAwake() {
  33. DebugMy.Log("[HashCode: " + GetHashCode() + "] " + "OnSCAwake", this);
  34. }
  35. /// <summary>
  36. /// Module 初始化 ===================================
  37. /// </summary>
  38. /// <summary>
  39. /// ModuleEnable ===================================
  40. /// </summary>
  41. [Obsolete("Should not use the EventFunction")]
  42. public void OnSCEnable() {
  43. DebugMy.Log("[HashCode: " + GetHashCode() + "] " + "OnSCEnable", this);
  44. }
  45. /// <summary>
  46. /// ModuleEnable ===================================
  47. /// </summary>
  48. /// <summary>
  49. /// Module 启动 ===================================
  50. /// </summary>
  51. public void ModuleStart() {
  52. if(!IsModuleInit) {
  53. DebugMy.Log("Please Invoke ModuleInit First", this);
  54. return;
  55. }
  56. if(IsModuleStarted) {
  57. DebugMy.Log("ModuleStart Had Invoke", this);
  58. return;
  59. }
  60. IsModuleStarted = true;
  61. if(IsEffectGameObject) {
  62. gameObject.SetActive(true);
  63. }
  64. OnSCStart();
  65. }
  66. public virtual void OnSCStart() {
  67. DebugMy.Log("[HashCode: " + GetHashCode() + "] " + "OnSCStart", this);
  68. }
  69. /// <summary>
  70. /// Module 启动 ===================================
  71. /// </summary>
  72. ///
  73. /// <summary>
  74. /// Module Update ===================================
  75. /// </summary>
  76. public void ModuleUpdate() {
  77. if(IsModuleInit == false || IsModuleStarted == false) {
  78. //DebugMy.Log("Pleaase Invoke ModuleInit First", this);
  79. return;
  80. }
  81. OnSCUpdate();
  82. }
  83. public virtual void OnSCUpdate() {
  84. lock(subModuleList) {
  85. foreach(var Module in subModuleList) {
  86. Module.ModuleUpdate();
  87. }
  88. }
  89. //DebugMy.Log("[HashCode: " + GetHashCode() + "] " + "OnSCUpdate", this);
  90. }
  91. /// <summary>
  92. /// Module Update ===================================
  93. /// </summary>
  94. /// <summary>
  95. /// Module LateUpdate ===================================
  96. /// </summary>
  97. public void ModuleLateUpdate() {
  98. if(IsModuleInit == false || IsModuleStarted == false) {
  99. //DebugMy.Log("Please Invoke ModuleInit First", this);
  100. return;
  101. }
  102. OnSCLateUpdate();
  103. }
  104. public virtual void OnSCLateUpdate() {
  105. lock(subModuleList) {
  106. foreach(var Module in subModuleList) {
  107. Module.ModuleLateUpdate();
  108. }
  109. }
  110. //DebugMy.Log("[HashCode: " + GetHashCode() + "] " + "OnSCLateUpdate", this);
  111. }
  112. /// <summary>
  113. /// Module LateUpdate ===================================
  114. /// </summary>
  115. /// <summary>
  116. /// ModuleEndOfFrame ===================================
  117. /// </summary>
  118. public void ModuleEndOfFrame() {
  119. if(IsModuleInit == false || IsModuleStarted == false) {
  120. //DebugMy.Log("Please Invoke ModuleInit First", this);
  121. return;
  122. }
  123. OnSCFuncitonWaitForEndOfFrame();
  124. }
  125. public virtual void OnSCFuncitonWaitForEndOfFrame() {
  126. lock(subModuleList) {
  127. foreach(var Module in subModuleList) {
  128. Module.ModuleEndOfFrame();
  129. }
  130. }
  131. //DebugMy.Log("[HashCode: " + GetHashCode() + "] " + "OnSCFuncitonWaitForEndOfFrame", this);
  132. }
  133. /// <summary>
  134. /// ModuleEndOfFrame ===================================
  135. /// </summary>
  136. /// <summary>
  137. /// ModuleStop ===================================
  138. /// </summary>
  139. public void ModuleStop() {
  140. if(IsModuleInit == false) {
  141. DebugMy.Log("ModuleStop: Please Invoke ModuleInit First", this);
  142. return;
  143. }
  144. if(IsModuleStarted == false) {
  145. DebugMy.Log("ModuleStop: Please Invoke ModuleStart First", this);
  146. return;
  147. }
  148. OnSCDisable();
  149. IsModuleStarted = false;
  150. if(IsEffectGameObject) {
  151. gameObject.SetActive(false);
  152. }
  153. }
  154. public virtual void OnSCDisable() {
  155. lock(subModuleList) {
  156. foreach(var Module in subModuleList) {
  157. Module.ModuleStop();
  158. }
  159. }
  160. DebugMy.Log("[HashCode: " + GetHashCode() + "] " + "OnSCDisable", this);
  161. }
  162. /// <summary>
  163. /// ModuleStop ===================================
  164. /// </summary>
  165. /// <summary>
  166. /// ModuleDestroy ===================================
  167. /// </summary>
  168. public void ModuleDestroy() {
  169. if(IsModuleInit == false) {
  170. DebugMy.Log("ModuleDestroy: Please Invoke ModuleInit First", this);
  171. return;
  172. }
  173. OnSCDestroy();
  174. IsModuleInit = false;
  175. IsModuleStarted = false;
  176. FatherModule = null;
  177. ModuleName = "";
  178. }
  179. public virtual void OnSCDestroy() {
  180. lock(subModuleList) {
  181. foreach(var Module in subModuleList) {
  182. Module.ModuleDestroy();
  183. }
  184. }
  185. subModuleList.Clear();
  186. DebugMy.Log("[HashCode: " + GetHashCode() + "] " + "OnSCDestroy", this);
  187. }
  188. /// <summary>
  189. /// ModuleDestroy ===================================
  190. /// </summary>
  191. public void AddModule(ISCModule Module, bool isEffectGameObject = true, SCModulePriority priority = SCModulePriority.Middle) {
  192. if(Module == null)
  193. return;
  194. Module.FatherModule = this;
  195. Module.ModuleInit(isEffectGameObject, priority);
  196. DebugMy.Log("[HashCode: " + GetHashCode() + "] " + "Module '" + Module.GetType() + "' [HashCode: " + Module.GetHashCode() + "] Add To Module '" + GetType(), this);
  197. lock(subModuleList) {
  198. subModuleList.Add(Module);
  199. }
  200. }
  201. public void RemoveModule(ISCModule Module) {
  202. if(Module == null)
  203. return;
  204. DebugMy.Log("[HashCode: " + GetHashCode() + "] " + "Module '" + Module.GetType() + "' [HashCode: " + Module.GetHashCode() + "] Remove From Module '" + GetType(), this);
  205. lock(subModuleList) {
  206. subModuleList.Remove(Module);
  207. }
  208. Module.ModuleDestroy();
  209. Module.FatherModule = null;
  210. }
  211. public void RemoveAllModule() {
  212. lock(subModuleList) {
  213. foreach(var item in subModuleList) {
  214. item.ModuleDestroy();
  215. }
  216. }
  217. DebugMy.Log("[HashCode: " + GetHashCode() + "] " + "Remove All Module '" + GetType(), this);
  218. subModuleList.Clear();
  219. }
  220. public virtual T GetSubModule<T>() where T : ISCModule {
  221. lock(subModuleList) {
  222. foreach(var Module in subModuleList) {
  223. if(typeof(T).ToString() == Module.ModuleName) {
  224. DebugMy.Log("[HashCode: " + GetHashCode() + "] Module" + typeof(T).ToString() + " Found ", this);
  225. return (T)Module;
  226. }
  227. }
  228. }
  229. DebugMy.Log("[HashCode: " + GetHashCode() + "] Module" + typeof(T).ToString() + " No Found ", this);
  230. return default(T);
  231. }
  232. /// <summary>
  233. /// 返回直接父
  234. /// </summary>
  235. /// <typeparam name="T"></typeparam>
  236. /// <returns></returns>
  237. public virtual T GetFatherModule<T>() where T : ISCModule {
  238. ISCModule father = FatherModule;
  239. while(father != null) {
  240. if(father.ModuleName == typeof(T).ToString()) {
  241. DebugMy.Log("[HashCode: " + GetHashCode() + "] FatherModule" + father.ModuleName + " Found ", this);
  242. return (T)father;
  243. }
  244. father = father.FatherModule;
  245. }
  246. DebugMy.Log("[HashCode: " + GetHashCode() + "] FatherModule" + typeof(T).ToString() + " Found ", this);
  247. return default(T);
  248. }
  249. public T Transition<T>(ISCModule data) where T : class {
  250. if((data as T) == null)
  251. throw new ArgumentException(String.Format("Invalid type: {0} passed to event expecting {1}", data.GetType(), typeof(T)));
  252. return data as T;
  253. }
  254. }
  255. }