GameObjectPool.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using CommonAI.Zone;
  2. using CommonAI.Zone.ZoneEditor;
  3. using CommonLang;
  4. using Sirenix.Utilities;
  5. using System.Collections.Generic;
  6. using System.Text.RegularExpressions;
  7. using UnityEngine;
  8. namespace ET.Client
  9. {
  10. [FriendOf(typeof(GameObjectPool))]
  11. public static class GameObjectPoolSystem
  12. {
  13. [ObjectSystem]
  14. public class GameObjectPoolAwakeSystem : AwakeSystem<GameObjectPool>
  15. {
  16. protected override void Awake(GameObjectPool self)
  17. {
  18. GameObjectPool.Instance = self;
  19. }
  20. }
  21. [ObjectSystem]
  22. public class GameObjectPoolDestroySystem : DestroySystem<GameObjectPool>
  23. {
  24. protected override void Destroy(GameObjectPool self)
  25. {
  26. self.ClearCache();
  27. GameObjectPool.Instance = null;
  28. }
  29. }
  30. }
  31. [ComponentOf(typeof(Scene))]
  32. public class GameObjectPool : Entity, IAwake, IDestroy
  33. {
  34. [StaticField]
  35. public static GameObjectPool Instance;
  36. private readonly HashMap<string, List<GameObject>> goPool = new();
  37. private readonly HashMap<string, AudioClip> audioPool = new();
  38. //TODO: 选取当前场景的单位、技能(事件触发等等)
  39. //TODO: BUFF
  40. public async ETTask CacheSceneObject(int scnId)
  41. {
  42. var mgr = BattleResourceMgr.Instance.GameEditorTemplates;
  43. var templates = mgr.Templates;
  44. SceneData sceneData = mgr.LoadScene(scnId, false, true);
  45. BattleMgr.Instance.Layer.Data = sceneData;
  46. List<string> effectlist = new();
  47. List<string> soundlist = new();
  48. if(!sceneData.BGM.IsNullOrWhitespace())
  49. {
  50. soundlist.Add(sceneData.BGM);
  51. }
  52. //遍历模型
  53. var units = templates.getUnits();
  54. foreach(var unit in units.Values)
  55. {
  56. var name = $"Unit_{unit.FileName}";
  57. var handle = await YooAssetProxy.LoadAssetAsync<GameObject>(name);
  58. var prefab = handle.GetAssetObject<GameObject>();
  59. var gameobj = UnityEngine.Object.Instantiate(prefab, GlobalViewComponent.Instance.RecycleNode, true);
  60. gameobj.name = name;
  61. RecycleObject(gameobj);
  62. if(unit.FootCircleEffect != null) CacheLaunchEffect(unit.FootCircleEffect, ref effectlist, ref soundlist);
  63. if (unit.SpawnEffect != null) CacheLaunchEffect(unit.SpawnEffect, ref effectlist, ref soundlist);
  64. if (unit.DeadActionEffect != null) CacheLaunchEffect(unit.DeadActionEffect, ref effectlist, ref soundlist);
  65. }
  66. var skills = templates.getAllSkillData();
  67. foreach (var skt in skills.Values)
  68. {
  69. foreach (var act in skt.ActionQueue)
  70. {
  71. if (!act.ActionEffectFileName.IsNullOrWhitespace())
  72. {
  73. effectlist.Add(act.ActionEffectFileName);
  74. }
  75. foreach (var kf in act.KeyFrames)
  76. {
  77. if (kf.Effect != null)
  78. {
  79. CacheLaunchEffect(kf.Effect, ref effectlist, ref soundlist);
  80. }
  81. if (kf.Spell != null)
  82. {
  83. CacheSpellEffect(templates, kf.Spell.SpellID, ref effectlist, ref soundlist);
  84. }
  85. }
  86. }
  87. }
  88. foreach (var ef in effectlist)
  89. {
  90. var name = $"Effect_{ef}";
  91. var handle = await YooAssetProxy.LoadAssetAsync<GameObject>(name);
  92. var prefab = handle.GetAssetObject<GameObject>();
  93. var gameobj = UnityEngine.Object.Instantiate(prefab, GlobalViewComponent.Instance.RecycleNode, true);
  94. gameobj.name = name;
  95. RecycleObject(gameobj);
  96. }
  97. foreach(var sound in soundlist)
  98. {
  99. var key = sound;
  100. var m = Regex.Match(key, "/?res/sound/.*/([\\w_\\d]+)\\.assetbundles$");
  101. if (m.Success)
  102. {
  103. key = m.Groups[1].Value.ToLower();
  104. }
  105. if(audioPool.ContainsKey(key))
  106. {
  107. continue;
  108. }
  109. //Log.Debug($"cache audio: {key}");
  110. var handle = await YooAssetProxy.LoadAssetAsync<AudioClip>($"Sound_{key}");
  111. var ac = handle.GetAssetObject<AudioClip>();
  112. ac.LoadAudioData();
  113. audioPool.Add(key, ac);
  114. }
  115. }
  116. private void CacheLaunchEffect(LaunchEffect effect, ref List<string>effectlist, ref List<string> soundlist)
  117. {
  118. if(!effect.Name.IsNullOrWhitespace())
  119. {
  120. effectlist.Add(effect.Name);
  121. }
  122. if(!effect.SoundName.IsNullOrWhitespace())
  123. {
  124. soundlist.Add(effect.SoundName);
  125. }
  126. }
  127. private void CacheSpellEffect(TemplateManager templates, int spellid, ref List<string> effectlist, ref List<string> soundlist)
  128. {
  129. var spell = templates.getSpell(spellid);
  130. if (spell == null)
  131. {
  132. Log.Error($"spell({spellid}) not exist");
  133. return;
  134. }
  135. if (!spell.FileName.IsNullOrWhitespace())
  136. {
  137. effectlist.Add(spell.FileName);
  138. }
  139. var frame = spell.HitIntervalKeyFrame;
  140. if (frame != null)
  141. {
  142. if (frame.Effect != null)
  143. {
  144. CacheLaunchEffect(frame.Effect, ref effectlist, ref soundlist);
  145. }
  146. if (frame.Spell != null && frame.Spell.SpellID != 0)
  147. {
  148. CacheSpellEffect(templates, frame.Spell.SpellID, ref effectlist, ref soundlist);
  149. }
  150. if (frame.Attack != null)
  151. {
  152. var attack = frame.Attack;
  153. if (attack.Effect != null)
  154. {
  155. CacheLaunchEffect(attack.Effect, ref effectlist, ref soundlist);
  156. }
  157. if (attack.Spell != null && attack.Spell.SpellID != 0)
  158. {
  159. CacheSpellEffect(templates, attack.Spell.SpellID, ref effectlist, ref soundlist);
  160. }
  161. }
  162. }
  163. frame = spell.HitOnExplosionKeyFrame;
  164. if (spell.HitOnExplosion && frame != null)
  165. {
  166. if (frame.Effect != null)
  167. {
  168. CacheLaunchEffect(frame.Effect, ref effectlist, ref soundlist);
  169. }
  170. if (frame.Spell != null && frame.Spell.SpellID != 0)
  171. {
  172. CacheSpellEffect(templates, frame.Spell.SpellID, ref effectlist, ref soundlist);
  173. }
  174. if (frame.Attack != null)
  175. {
  176. var attack = frame.Attack;
  177. if (attack.Effect != null)
  178. {
  179. CacheLaunchEffect(attack.Effect, ref effectlist, ref soundlist);
  180. }
  181. if (attack.Spell != null && attack.Spell.SpellID != 0)
  182. {
  183. CacheSpellEffect(templates, attack.Spell.SpellID, ref effectlist, ref soundlist);
  184. }
  185. }
  186. }
  187. foreach (var frm in spell.KeyFrames)
  188. {
  189. if (frm.Effect != null)
  190. {
  191. CacheLaunchEffect(frm.Effect, ref effectlist, ref soundlist);
  192. }
  193. if (frm.Spell != null && frame.Spell.SpellID != 0)
  194. {
  195. CacheSpellEffect(templates, frm.Spell.SpellID, ref effectlist, ref soundlist);
  196. }
  197. if (frm.Attack != null)
  198. {
  199. var attack = frm.Attack;
  200. if (attack.Effect != null)
  201. {
  202. CacheLaunchEffect(attack.Effect, ref effectlist, ref soundlist);
  203. }
  204. if (attack.Spell != null && attack.Spell.SpellID != 0)
  205. {
  206. CacheSpellEffect(templates, attack.Spell.SpellID, ref effectlist, ref soundlist);
  207. }
  208. }
  209. }
  210. }
  211. public void ClearCache()
  212. {
  213. foreach (var list in goPool.Values)
  214. {
  215. foreach (var go in list)
  216. {
  217. GameObject.Destroy(go);
  218. }
  219. }
  220. goPool.Clear();
  221. foreach(var ac in audioPool.Values)
  222. {
  223. ac.UnloadAudioData();
  224. }
  225. audioPool.Clear();
  226. }
  227. public void RecycleObject(GameObject go, string key = "")
  228. {
  229. if(key.IsNullOrWhitespace())
  230. {
  231. key = go.name;
  232. }
  233. //Log.Debug($"recycle gameobject: {key}");
  234. go.SetActive(false);
  235. go.transform.SetParent(GlobalViewComponent.Instance.RecycleNode, false);
  236. if (goPool.TryGetValue(key, out var golist))
  237. {
  238. golist.Add(go);
  239. }
  240. else
  241. {
  242. goPool.Add(key, new List<GameObject>() { go });
  243. }
  244. }
  245. public async ETTask<GameObject> Acquire(string key)
  246. {
  247. if (goPool.TryGetValue(key, out var golist))
  248. {
  249. if (golist.Count > 0)
  250. {
  251. var gobj = golist[0];
  252. golist.RemoveRange(0, 1);
  253. return gobj;
  254. }
  255. }
  256. else
  257. {
  258. goPool.Add(key, new List<GameObject>());
  259. }
  260. Log.Warning($"not cache gameobject: ({key})");
  261. var handle = await YooAssetProxy.LoadAssetAsync<GameObject>(key);
  262. var prefab = handle.GetAssetObject<GameObject>();
  263. var gameobj = UnityEngine.Object.Instantiate(prefab, GlobalViewComponent.Instance.RecycleNode, true);
  264. gameobj.name = key;
  265. return gameobj;
  266. }
  267. public async ETTask<AudioClip> AcquireSound(string key)
  268. {
  269. if(audioPool.TryGetValue(key, out var ac))
  270. {
  271. return ac;
  272. }
  273. Log.Warning($"not cache audio: ({key})");
  274. var handle = await YooAssetProxy.LoadAssetAsync<AudioClip>($"Sound_{key}");
  275. var aac = handle.GetAssetObject<AudioClip>();
  276. aac.LoadAudioData();
  277. audioPool.Add(key, aac);
  278. return aac;
  279. }
  280. }
  281. }