Unity 框架

ObjectPoolMananger.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace TFramework
  5. {
  6. public class ObjectPoolMananger : BaseManager
  7. {
  8. public Transform m_poolTransform;
  9. private Dictionary<string, ObjectPool> _poolDic = new Dictionary<string, ObjectPool>();
  10. /// <summary>
  11. /// 注册对象池
  12. /// </summary>
  13. /// <param name="poolName">对象池名字</param>
  14. /// <param name="objTemplate">对象模板</param>
  15. /// <param name="maxCount">池子最大量</param>
  16. /// <param name="takeOutEvent">取出事件</param>
  17. /// <param name="recycleEvent">回收事件</param>
  18. public void RegisterPool(string poolName, GameObject objTemplate, int maxCount, TAction<GameObject> takeOutEvent = null, TAction<GameObject> recycleEvent = null)
  19. {
  20. if (_poolDic.ContainsKey(poolName))
  21. Log.Error($"对象池【{poolName}】已注册,请勿重复注册!");
  22. else
  23. {
  24. new GameObject(poolName).transform.parent = m_poolTransform;
  25. _poolDic.Add(poolName, new ObjectPool(objTemplate, maxCount, takeOutEvent, recycleEvent));
  26. }
  27. }
  28. /// <summary>
  29. /// 取出对象
  30. /// </summary>
  31. /// <param name="poolName"></param>
  32. /// <returns></returns>
  33. public GameObject TakeOutObj(string poolName)
  34. {
  35. if (_poolDic.ContainsKey(poolName))
  36. return _poolDic[poolName].TakeOutObj();
  37. else
  38. {
  39. Log.Error($"不存在名字为【{poolName}】的对象池!");
  40. return null;
  41. }
  42. }
  43. /// <summary>
  44. /// 回收对象
  45. /// </summary>
  46. /// <param name="poolName">对象池名字</param>
  47. /// <param name="obj">对象</param>
  48. public void RecycleObj(string poolName, GameObject obj)
  49. {
  50. if (_poolDic.ContainsKey(poolName))
  51. {
  52. obj.transform.SetParent(m_poolTransform.Find(poolName),false);
  53. _poolDic[poolName].RecycleObj(obj);
  54. }
  55. else
  56. Log.Error($"不存在名字为【{poolName}】的对象池!");
  57. }
  58. /// <summary>
  59. /// 批量回收对象
  60. /// </summary>
  61. /// <param name="poolName"></param>
  62. /// <param name="objs"></param>
  63. public void RecycleObjs(string poolName, IEnumerable<GameObject> objs)
  64. {
  65. foreach (var item in objs)
  66. {
  67. RecycleObj(poolName, item);
  68. }
  69. }
  70. /// <summary>
  71. /// 检查是否存在对象池
  72. /// </summary>
  73. /// <param name="poolName"></param>
  74. /// <returns></returns>
  75. public bool IsHaveObjPool(string poolName) => _poolDic.ContainsKey(poolName);
  76. /// <summary>
  77. /// 清空指定对象池
  78. /// </summary>
  79. public void ClearObjpool(string poolName)
  80. {
  81. if (IsHaveObjPool(poolName))
  82. _poolDic[poolName].Clear();
  83. }
  84. /// <summary>
  85. /// 清空所有对象池
  86. /// </summary>
  87. public void ClearAll()
  88. {
  89. foreach (var item in _poolDic)
  90. item.Value.Clear();
  91. }
  92. }
  93. }