GameObjectPool.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. namespace ET.Client
  6. {
  7. public class GameObjectPool : Singleton<GameObjectPool>
  8. {
  9. private readonly Dictionary<string, AudioClip> audioPool = new();
  10. private readonly Dictionary<string, Sprite> cardSpritePool = new();
  11. //scroll item list
  12. public List<GameObject> fightCardItemPools = new List<GameObject>();
  13. public List<GameObject> disCardItemPools = new List<GameObject>();
  14. public List<GameObject> chatFastItemPools = new List<GameObject>();
  15. public List<GameObject> chatFaceItemPools = new List<GameObject>();
  16. public void ClearCache()
  17. {
  18. foreach(var ac in audioPool.Values)
  19. {
  20. ac.UnloadAudioData();
  21. }
  22. audioPool.Clear();
  23. cardSpritePool.Clear();
  24. disCardItemPools.Clear();
  25. fightCardItemPools.Clear();
  26. chatFastItemPools.Clear();
  27. }
  28. public async ETTask<AudioClip> AcquireSound(string abName)
  29. {
  30. if(audioPool.TryGetValue(abName, out var ac))
  31. {
  32. return ac;
  33. }
  34. //Log.Warning($"not cache audio: ({key})");
  35. await ResourcesComponentHelper.Instance.LoadAssetAsync(GameUtil.Instance.GetSceneComponent(), "fightsound");
  36. UnityEngine.Object soundObj = ResourcesComponentHelper.Instance.GetAssetAsync("fightsound", abName);
  37. var aac = soundObj as AudioClip ;
  38. aac.LoadAudioData();
  39. audioPool.Add(abName, aac);
  40. return aac;
  41. }
  42. public async ETTask<Sprite> AcquireSprite(string abName)
  43. {
  44. if (cardSpritePool.TryGetValue(abName, out var sp))
  45. {
  46. return sp;
  47. }
  48. await ResourcesComponentHelper.Instance.LoadAssetAsync(GameUtil.Instance.GetSceneComponent(), "majiangatlas");
  49. UnityEngine.Object spriteObj = ResourcesComponentHelper.Instance.GetAssetAsync("majiangatlas", abName);
  50. Texture2D texture = spriteObj as Texture2D;
  51. var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100f);
  52. cardSpritePool.Add(abName, sprite);
  53. return sprite;
  54. }
  55. }
  56. }