GameObjectPool.cs 13 KB

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