GameObjectPool.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using CommonAI.Zone;
  2. using CommonLang;
  3. using Sirenix.Utilities;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace ET.Client
  7. {
  8. [FriendOf(typeof(GameObjectPool))]
  9. public static class GameObjectPoolSystem
  10. {
  11. [ObjectSystem]
  12. public class GameObjectPoolAwakeSystem : AwakeSystem<GameObjectPool>
  13. {
  14. protected override void Awake(GameObjectPool self)
  15. {
  16. GameObjectPool.Instance = self;
  17. }
  18. }
  19. [ObjectSystem]
  20. public class GameObjectPoolDestroySystem : DestroySystem<GameObjectPool>
  21. {
  22. protected override void Destroy(GameObjectPool self)
  23. {
  24. self.ClearCache();
  25. GameObjectPool.Instance = null;
  26. }
  27. }
  28. }
  29. [ComponentOf(typeof(Scene))]
  30. public class GameObjectPool : Entity, IAwake, IDestroy
  31. {
  32. [StaticField]
  33. public static GameObjectPool Instance;
  34. private readonly HashMap<string, List<GameObject>> goPool = new();
  35. //TODO: 选取当前场景的单位、技能(事件触发等等)
  36. //TODO: BUFF
  37. public async ETTask CacheSceneObject(/*int scnId*/)
  38. {
  39. var templates = BattleResourceMgr.Instance.GameEditorTemplates.Templates;
  40. //遍历模型
  41. var units = templates.getUnits();
  42. foreach(var unit in units.Values)
  43. {
  44. var name = $"Unit_{unit.FileName}";
  45. var handle = await YooAssetProxy.LoadAssetAsync<GameObject>(name);
  46. var prefab = handle.GetAssetObject<GameObject>();
  47. var gameobj = UnityEngine.Object.Instantiate(prefab, GlobalViewComponent.Instance.RecycleNode, true);
  48. gameobj.name = name;
  49. RecycleObject(gameobj);
  50. }
  51. List<string> effectlist = new();
  52. var skills = templates.getAllSkillData();
  53. foreach (var skt in skills.Values)
  54. {
  55. foreach (var act in skt.ActionQueue)
  56. {
  57. if (!act.ActionEffectFileName.IsNullOrWhitespace())
  58. {
  59. effectlist.Add(act.ActionEffectFileName);
  60. }
  61. foreach (var kf in act.KeyFrames)
  62. {
  63. if (kf.Effect != null && !kf.Effect.Name.IsNullOrWhitespace())
  64. {
  65. effectlist.Add(kf.Effect.Name);
  66. }
  67. if (kf.Spell != null)
  68. {
  69. CacheSpellEffect(templates, kf.Spell.SpellID, ref effectlist);
  70. }
  71. }
  72. }
  73. }
  74. foreach (var ef in effectlist)
  75. {
  76. var name = $"Effect_{ef}";
  77. var handle = await YooAssetProxy.LoadAssetAsync<GameObject>(name);
  78. var prefab = handle.GetAssetObject<GameObject>();
  79. var gameobj = UnityEngine.Object.Instantiate(prefab, GlobalViewComponent.Instance.RecycleNode, true);
  80. gameobj.name = name;
  81. RecycleObject(gameobj);
  82. }
  83. }
  84. private void CacheSpellEffect(TemplateManager templates, int spellid, ref List<string> effectlist)
  85. {
  86. var spell = templates.getSpell(spellid);
  87. if (spell == null)
  88. {
  89. Log.Error($"spell({spellid}) not exist");
  90. return;
  91. }
  92. if (!spell.FileName.IsNullOrWhitespace())
  93. {
  94. effectlist.Add(spell.FileName);
  95. }
  96. var frame = spell.HitIntervalKeyFrame;
  97. if (frame != null)
  98. {
  99. if (frame.Effect != null && !frame.Effect.Name.IsNullOrWhitespace())
  100. {
  101. effectlist.Add(frame.Effect.Name);
  102. }
  103. if (frame.Spell != null && frame.Spell.SpellID != 0)
  104. {
  105. CacheSpellEffect(templates, frame.Spell.SpellID, ref effectlist);
  106. }
  107. if (frame.Attack != null)
  108. {
  109. var attack = frame.Attack;
  110. if (attack.Effect != null && !attack.Effect.Name.IsNullOrWhitespace())
  111. {
  112. effectlist.Add(attack.Effect.Name);
  113. }
  114. if (attack.Spell != null && attack.Spell.SpellID != 0)
  115. {
  116. CacheSpellEffect(templates, attack.Spell.SpellID, ref effectlist);
  117. }
  118. }
  119. }
  120. frame = spell.HitOnExplosionKeyFrame;
  121. if (spell.HitOnExplosion && frame != null)
  122. {
  123. if (frame.Effect != null && !frame.Effect.Name.IsNullOrWhitespace())
  124. {
  125. effectlist.Add(frame.Effect.Name);
  126. }
  127. if (frame.Spell != null && frame.Spell.SpellID != 0)
  128. {
  129. CacheSpellEffect(templates, frame.Spell.SpellID, ref effectlist);
  130. }
  131. if (frame.Attack != null)
  132. {
  133. var attack = frame.Attack;
  134. if (attack.Effect != null && !attack.Effect.Name.IsNullOrWhitespace())
  135. {
  136. effectlist.Add(attack.Effect.Name);
  137. }
  138. if (attack.Spell != null && attack.Spell.SpellID != 0)
  139. {
  140. CacheSpellEffect(templates, attack.Spell.SpellID, ref effectlist);
  141. }
  142. }
  143. }
  144. foreach (var frm in spell.KeyFrames)
  145. {
  146. if (frm.Effect != null && !frm.Effect.Name.IsNullOrWhitespace())
  147. {
  148. effectlist.Add(frm.Effect.Name);
  149. }
  150. if (frm.Spell != null && frame.Spell.SpellID != 0)
  151. {
  152. CacheSpellEffect(templates, frm.Spell.SpellID, ref effectlist);
  153. }
  154. if (frm.Attack != null)
  155. {
  156. var attack = frm.Attack;
  157. if (attack.Effect != null && !attack.Effect.Name.IsNullOrWhitespace())
  158. {
  159. effectlist.Add(attack.Effect.Name);
  160. }
  161. if (attack.Spell != null && attack.Spell.SpellID != 0)
  162. {
  163. CacheSpellEffect(templates, attack.Spell.SpellID, ref effectlist);
  164. }
  165. }
  166. }
  167. }
  168. public void ClearCache()
  169. {
  170. foreach (var list in goPool.Values)
  171. {
  172. foreach (var go in list)
  173. {
  174. GameObject.Destroy(go);
  175. }
  176. }
  177. goPool.Clear();
  178. }
  179. public void RecycleObject(GameObject go, string key = "")
  180. {
  181. if(key.IsNullOrWhitespace())
  182. {
  183. key = go.name;
  184. }
  185. Log.Debug($"cache gameobject: {key}");
  186. go.SetActive(false);
  187. go.transform.SetParent(GlobalViewComponent.Instance.RecycleNode, false);
  188. if (goPool.TryGetValue(key, out var golist))
  189. {
  190. golist.Add(go);
  191. }
  192. else
  193. {
  194. goPool.Add(key, new List<GameObject>() { go });
  195. }
  196. }
  197. public async ETTask<GameObject> Acquire(string key)
  198. {
  199. if (goPool.TryGetValue(key, out var golist))
  200. {
  201. if (golist.Count > 0)
  202. {
  203. Log.Debug($"got gameobject ({key}) from pool");
  204. var gobj = golist[0];
  205. golist.RemoveRange(0, 1);
  206. return gobj;
  207. }
  208. }
  209. else
  210. {
  211. goPool.Add(key, new List<GameObject>());
  212. }
  213. Log.Debug($"new gameobject: ({key})");
  214. var handle = await YooAssetProxy.LoadAssetAsync<GameObject>(key);
  215. var prefab = handle.GetAssetObject<GameObject>();
  216. var gameobj = UnityEngine.Object.Instantiate(prefab, GlobalViewComponent.Instance.RecycleNode, true);
  217. gameobj.name = key;
  218. return gameobj;
  219. }
  220. }
  221. }