SCModule.cs 9.8 KB

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