GameObjectPool.cs 12 KB

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