GameObjectPool.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.DeadActionEffect != null) CacheLaunchEffect(unit.DeadActionEffect, 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.PreloadResources.IsNullOrWhitespace() )
  74. {
  75. var split = unit.PreloadResources.Split(';');
  76. foreach(var res in split)
  77. {
  78. var realres = res;
  79. var cns = res.Split('|');
  80. var cnt = 1;
  81. if (cns.Length > 1)
  82. {
  83. realres = cns[0];
  84. try
  85. {
  86. cnt = Convert.ToInt32(cns[1]);
  87. }
  88. catch
  89. {
  90. Log.Error($"PreloadResources illegal: {unit.PreloadResources}");
  91. break;
  92. }
  93. }
  94. for (var i = 0; i < cnt; i++)
  95. {
  96. await CachePrefab(realres);
  97. }
  98. }
  99. }
  100. }
  101. var skills = templates.getAllSkillData();
  102. foreach (var skt in skills.Values)
  103. {
  104. foreach (var act in skt.ActionQueue)
  105. {
  106. if (!act.ActionEffectFileName.IsNullOrWhitespace())
  107. {
  108. effectlist.Add(act.ActionEffectFileName);
  109. }
  110. foreach (var kf in act.KeyFrames)
  111. {
  112. if (kf.Effect != null)
  113. {
  114. CacheLaunchEffect(kf.Effect, ref effectlist, ref soundlist);
  115. }
  116. if (kf.Spell != null)
  117. {
  118. CacheSpellEffect(templates, kf.Spell.SpellID, ref effectlist, ref soundlist);
  119. }
  120. }
  121. }
  122. }
  123. foreach (var ef in effectlist)
  124. {
  125. await CachePrefab($"Effect_{ef}");
  126. }
  127. foreach(var sound in soundlist)
  128. {
  129. var key = sound;
  130. var m = Regex.Match(key, "/?res/sound/.*/([\\w_\\d]+)\\.assetbundles$");
  131. if (m.Success)
  132. {
  133. key = m.Groups[1].Value.ToLower();
  134. }
  135. if(audioPool.ContainsKey(key))
  136. {
  137. continue;
  138. }
  139. //Log.Debug($"cache audio: {key}");
  140. var handle = await YooAssetProxy.LoadAssetAsync<AudioClip>($"Sound_{key}");
  141. var ac = handle.GetAssetObject<AudioClip>();
  142. ac.LoadAudioData();
  143. audioPool.Add(key, ac);
  144. }
  145. }
  146. private void CacheLaunchEffect(LaunchEffect effect, ref List<string>effectlist, ref List<string> soundlist)
  147. {
  148. if(!effect.Name.IsNullOrWhitespace())
  149. {
  150. effectlist.Add(effect.Name);
  151. }
  152. if(!effect.SoundName.IsNullOrWhitespace())
  153. {
  154. soundlist.Add(effect.SoundName);
  155. }
  156. }
  157. private void CacheSpellEffect(TemplateManager templates, int spellid, ref List<string> effectlist, ref List<string> soundlist)
  158. {
  159. var spell = templates.getSpell(spellid);
  160. if (spell == null)
  161. {
  162. Log.Error($"spell({spellid}) not exist");
  163. return;
  164. }
  165. if (!spell.FileName.IsNullOrWhitespace())
  166. {
  167. effectlist.Add(spell.FileName);
  168. }
  169. var frame = spell.HitIntervalKeyFrame;
  170. if (frame != null)
  171. {
  172. if (frame.Effect != null)
  173. {
  174. CacheLaunchEffect(frame.Effect, ref effectlist, ref soundlist);
  175. }
  176. if (frame.Spell != null && frame.Spell.SpellID != 0)
  177. {
  178. CacheSpellEffect(templates, frame.Spell.SpellID, ref effectlist, ref soundlist);
  179. }
  180. if (frame.Attack != null)
  181. {
  182. var attack = frame.Attack;
  183. if (attack.Effect != null)
  184. {
  185. CacheLaunchEffect(attack.Effect, ref effectlist, ref soundlist);
  186. }
  187. if (attack.Spell != null && attack.Spell.SpellID != 0)
  188. {
  189. CacheSpellEffect(templates, attack.Spell.SpellID, ref effectlist, ref soundlist);
  190. }
  191. }
  192. }
  193. frame = spell.HitOnExplosionKeyFrame;
  194. if (spell.HitOnExplosion && frame != null)
  195. {
  196. if (frame.Effect != null)
  197. {
  198. CacheLaunchEffect(frame.Effect, ref effectlist, ref soundlist);
  199. }
  200. if (frame.Spell != null && frame.Spell.SpellID != 0)
  201. {
  202. CacheSpellEffect(templates, frame.Spell.SpellID, ref effectlist, ref soundlist);
  203. }
  204. if (frame.Attack != null)
  205. {
  206. var attack = frame.Attack;
  207. if (attack.Effect != null)
  208. {
  209. CacheLaunchEffect(attack.Effect, ref effectlist, ref soundlist);
  210. }
  211. if (attack.Spell != null && attack.Spell.SpellID != 0)
  212. {
  213. CacheSpellEffect(templates, attack.Spell.SpellID, ref effectlist, ref soundlist);
  214. }
  215. }
  216. }
  217. foreach (var frm in spell.KeyFrames)
  218. {
  219. if (frm.Effect != null)
  220. {
  221. CacheLaunchEffect(frm.Effect, ref effectlist, ref soundlist);
  222. }
  223. if (frm.Spell != null && frm.Spell.SpellID != 0)
  224. {
  225. CacheSpellEffect(templates, frm.Spell.SpellID, ref effectlist, ref soundlist);
  226. }
  227. if (frm.Attack != null)
  228. {
  229. var attack = frm.Attack;
  230. if (attack.Effect != null)
  231. {
  232. CacheLaunchEffect(attack.Effect, ref effectlist, ref soundlist);
  233. }
  234. if (attack.Spell != null && attack.Spell.SpellID != 0)
  235. {
  236. CacheSpellEffect(templates, attack.Spell.SpellID, ref effectlist, ref soundlist);
  237. }
  238. }
  239. }
  240. }
  241. public void ClearCache()
  242. {
  243. foreach (var list in goPool.Values)
  244. {
  245. foreach (var go in list)
  246. {
  247. GameObject.Destroy(go);
  248. }
  249. }
  250. goPool.Clear();
  251. foreach(var ac in audioPool.Values)
  252. {
  253. ac.UnloadAudioData();
  254. }
  255. audioPool.Clear();
  256. }
  257. public void RecycleObject(GameObject go, string key = "")
  258. {
  259. if(key.IsNullOrWhitespace())
  260. {
  261. key = go.name;
  262. }
  263. //Log.Debug($"recycle gameobject: {key}");
  264. go.SetActive(false);
  265. go.transform.SetParent(GlobalViewComponent.Instance.RecycleNode, false);
  266. if (goPool.TryGetValue(key, out var golist))
  267. {
  268. golist.Add(go);
  269. }
  270. else
  271. {
  272. goPool.Add(key, new List<GameObject>() { go });
  273. }
  274. }
  275. public async ETTask<GameObject> Acquire(string key)
  276. {
  277. if (goPool.TryGetValue(key, out var golist))
  278. {
  279. if (golist.Count > 0)
  280. {
  281. var gobj = golist[0];
  282. golist.RemoveRange(0, 1);
  283. return gobj;
  284. }
  285. }
  286. else
  287. {
  288. goPool.Add(key, new List<GameObject>());
  289. }
  290. Log.Warning($"not cache gameobject: ({key})");
  291. var handle = await YooAssetProxy.LoadAssetAsync<GameObject>(key);
  292. var prefab = handle.GetAssetObject<GameObject>();
  293. var gameobj = UnityEngine.Object.Instantiate(prefab, GlobalViewComponent.Instance.RecycleNode, true);
  294. gameobj.name = key;
  295. return gameobj;
  296. }
  297. public async ETTask<AudioClip> AcquireSound(string key)
  298. {
  299. if(audioPool.TryGetValue(key, out var ac))
  300. {
  301. return ac;
  302. }
  303. Log.Warning($"not cache audio: ({key})");
  304. var handle = await YooAssetProxy.LoadAssetAsync<AudioClip>($"Sound_{key}");
  305. var aac = handle.GetAssetObject<AudioClip>();
  306. aac.LoadAudioData();
  307. audioPool.Add(key, aac);
  308. return aac;
  309. }
  310. public async ETTask<GComponent> AcquireHeadBar()
  311. {
  312. if(headBarPool.Count > 0)
  313. {
  314. var ret = headBarPool[0];
  315. headBarPool.RemoveAt(0);
  316. return ret;
  317. }
  318. var root = GlobalViewComponent.Instance.HeadbarView;
  319. if (root == null)
  320. {
  321. //加入所有headbar的parent view
  322. root = await UIHelper.Create("HeadBar", "HeadBarRoot", 0);
  323. GlobalViewComponent.Instance.HeadbarView = root;
  324. root.visible = false;
  325. }
  326. var view = await UIHelper.Create("HeadBar", "normalbar", -1, root);
  327. return view;
  328. }
  329. public void RecycleHeadBar(GComponent view)
  330. {
  331. headBarPool.Add(view);
  332. }
  333. }
  334. }