ComponentFactory.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. namespace KYFramework
  3. {
  4. public static class ComponentFactory
  5. {
  6. public static Component CreateWithParent(Type type, Component parent, bool fromPool = true)
  7. {
  8. Component component;
  9. if (fromPool)
  10. {
  11. component = Game.ObjectPool.Fetch(type) as Component;
  12. }
  13. else
  14. {
  15. component = (Component)Activator.CreateInstance(type);
  16. }
  17. Game.EventSystem.Add(component);
  18. component.Parent = parent;
  19. if (component is ComponentWithId componentWithId)
  20. {
  21. componentWithId.Id = component.InstanceId;
  22. }
  23. Game.EventSystem.Awake(component);
  24. return component;
  25. }
  26. public static T CreateWithParent<T>(Component parent, bool fromPool = true) where T : Component
  27. {
  28. Type type = typeof(T);
  29. T component;
  30. if (fromPool)
  31. {
  32. component = (T)Game.ObjectPool.Fetch(type);
  33. }
  34. else
  35. {
  36. component = (T)Activator.CreateInstance(type);
  37. }
  38. Game.EventSystem.Add(component);
  39. component.Parent = parent;
  40. if (component is ComponentWithId componentWithId)
  41. {
  42. componentWithId.Id = component.InstanceId;
  43. }
  44. Game.EventSystem.Awake(component);
  45. return component;
  46. }
  47. public static T CreateWithParent<T, A>(Component parent, A a, bool fromPool = true) where T : Component
  48. {
  49. Type type = typeof(T);
  50. T component;
  51. if (fromPool)
  52. {
  53. component = (T)Game.ObjectPool.Fetch(type);
  54. }
  55. else
  56. {
  57. component = (T)Activator.CreateInstance(type);
  58. }
  59. Game.EventSystem.Add(component);
  60. component.Parent = parent;
  61. if (component is ComponentWithId componentWithId)
  62. {
  63. componentWithId.Id = component.InstanceId;
  64. }
  65. Game.EventSystem.Awake(component, a);
  66. return component;
  67. }
  68. public static T CreateWithParent<T, A, B>(Component parent, A a, B b, bool fromPool = true) where T : Component
  69. {
  70. Type type = typeof(T);
  71. T component;
  72. if (fromPool)
  73. {
  74. component = (T)Game.ObjectPool.Fetch(type);
  75. }
  76. else
  77. {
  78. component = (T)Activator.CreateInstance(type);
  79. }
  80. Game.EventSystem.Add(component);
  81. component.Parent = parent;
  82. if (component is ComponentWithId componentWithId)
  83. {
  84. componentWithId.Id = component.InstanceId;
  85. }
  86. Game.EventSystem.Awake(component, a, b);
  87. return component;
  88. }
  89. public static T CreateWithParent<T, A, B, C>(Component parent, A a, B b, C c, bool fromPool = true) where T : Component
  90. {
  91. Type type = typeof(T);
  92. T component;
  93. if (fromPool)
  94. {
  95. component = (T)Game.ObjectPool.Fetch(type);
  96. }
  97. else
  98. {
  99. component = (T)Activator.CreateInstance(type);
  100. }
  101. Game.EventSystem.Add(component);
  102. component.Parent = parent;
  103. if (component is ComponentWithId componentWithId)
  104. {
  105. componentWithId.Id = component.InstanceId;
  106. }
  107. Game.EventSystem.Awake(component, a, b, c);
  108. return component;
  109. }
  110. public static T Create<T>(bool fromPool = true) where T : Component
  111. {
  112. Type type = typeof(T);
  113. T component;
  114. if (fromPool)
  115. {
  116. component = (T)Game.ObjectPool.Fetch(type);
  117. }
  118. else
  119. {
  120. component = (T)Activator.CreateInstance(type);
  121. }
  122. Game.EventSystem.Add(component);
  123. if (component is ComponentWithId componentWithId)
  124. {
  125. componentWithId.Id = component.InstanceId;
  126. }
  127. Game.EventSystem.Awake(component);
  128. return component;
  129. }
  130. public static T Create<T, A>(A a, bool fromPool = true) where T : Component
  131. {
  132. Type type = typeof(T);
  133. T component;
  134. if (fromPool)
  135. {
  136. component = (T)Game.ObjectPool.Fetch(type);
  137. }
  138. else
  139. {
  140. component = (T)Activator.CreateInstance(type);
  141. }
  142. Game.EventSystem.Add(component);
  143. if (component is ComponentWithId componentWithId)
  144. {
  145. componentWithId.Id = component.InstanceId;
  146. }
  147. Game.EventSystem.Awake(component, a);
  148. return component;
  149. }
  150. public static T Create<T, A, B>(A a, B b, bool fromPool = true) where T : Component
  151. {
  152. Type type = typeof(T);
  153. T component;
  154. if (fromPool)
  155. {
  156. component = (T)Game.ObjectPool.Fetch(type);
  157. }
  158. else
  159. {
  160. component = (T)Activator.CreateInstance(type);
  161. }
  162. Game.EventSystem.Add(component);
  163. if (component is ComponentWithId componentWithId)
  164. {
  165. componentWithId.Id = component.InstanceId;
  166. }
  167. Game.EventSystem.Awake(component, a, b);
  168. return component;
  169. }
  170. public static T Create<T, A, B, C>(A a, B b, C c, bool fromPool = true) where T : Component
  171. {
  172. Type type = typeof(T);
  173. T component;
  174. if (fromPool)
  175. {
  176. component = (T)Game.ObjectPool.Fetch(type);
  177. }
  178. else
  179. {
  180. component = (T)Activator.CreateInstance(type);
  181. }
  182. Game.EventSystem.Add(component);
  183. if (component is ComponentWithId componentWithId)
  184. {
  185. componentWithId.Id = component.InstanceId;
  186. }
  187. Game.EventSystem.Awake(component, a, b, c);
  188. return component;
  189. }
  190. public static T CreateWithId<T>(long id, bool fromPool = true) where T : ComponentWithId
  191. {
  192. Type type = typeof(T);
  193. T component;
  194. if (fromPool)
  195. {
  196. component = (T)Game.ObjectPool.Fetch(type);
  197. }
  198. else
  199. {
  200. component = (T)Activator.CreateInstance(type);
  201. }
  202. Game.EventSystem.Add(component);
  203. component.Id = id;
  204. Game.EventSystem.Awake(component);
  205. return component;
  206. }
  207. public static T CreateWithId<T, A>(long id, A a, bool fromPool = true) where T : ComponentWithId
  208. {
  209. Type type = typeof(T);
  210. T component;
  211. if (fromPool)
  212. {
  213. component = (T)Game.ObjectPool.Fetch(type);
  214. }
  215. else
  216. {
  217. component = (T)Activator.CreateInstance(type);
  218. }
  219. Game.EventSystem.Add(component);
  220. component.Id = id;
  221. Game.EventSystem.Awake(component, a);
  222. return component;
  223. }
  224. public static T CreateWithId<T, A, B>(long id, A a, B b, bool fromPool = true) where T : ComponentWithId
  225. {
  226. Type type = typeof(T);
  227. T component;
  228. if (fromPool)
  229. {
  230. component = (T)Game.ObjectPool.Fetch(type);
  231. }
  232. else
  233. {
  234. component = (T)Activator.CreateInstance(type);
  235. }
  236. Game.EventSystem.Add(component);
  237. component.Id = id;
  238. Game.EventSystem.Awake(component, a, b);
  239. return component;
  240. }
  241. public static T CreateWithId<T, A, B, C>(long id, A a, B b, C c, bool fromPool = true) where T : ComponentWithId
  242. {
  243. Type type = typeof(T);
  244. T component;
  245. if (fromPool)
  246. {
  247. component = (T)Game.ObjectPool.Fetch(type);
  248. }
  249. else
  250. {
  251. component = (T)Activator.CreateInstance(type);
  252. }
  253. Game.EventSystem.Add(component);
  254. component.Id = id;
  255. Game.EventSystem.Awake(component, a, b, c);
  256. return component;
  257. }
  258. }
  259. }