GameObjectPool.cs 12 KB

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