a61bc3ba3fb43f9bad46eb552a7a4a7650b1f404.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, Component, instantiate, Prefab, Vec3, _dec, _class, _class2, _crd, ccclass, property, PoolManager;
  4. return {
  5. setters: [function (_cc) {
  6. _cclegacy = _cc.cclegacy;
  7. __checkObsolete__ = _cc.__checkObsolete__;
  8. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  9. _decorator = _cc._decorator;
  10. Component = _cc.Component;
  11. instantiate = _cc.instantiate;
  12. Prefab = _cc.Prefab;
  13. Vec3 = _cc.Vec3;
  14. }],
  15. execute: function () {
  16. _crd = true;
  17. _cclegacy._RF.push({}, "43f17BYDINONr03pwdby0M9", "PoolManager", undefined);
  18. __checkObsolete__(['_decorator', 'Component', 'instantiate', 'Node', 'Prefab', 'Vec3']);
  19. ({
  20. ccclass,
  21. property
  22. } = _decorator);
  23. /**
  24. * 对象池管理器
  25. * 功能:
  26. * 1. 管理游戏对象的复用
  27. * 2. 减少频繁创建/销毁对象的性能开销
  28. * 3. 支持Prefab和Node两种类型的对象池
  29. */
  30. _export("PoolManager", PoolManager = (_dec = ccclass('PoolManager'), _dec(_class = (_class2 = class PoolManager extends Component {
  31. /**
  32. * 从对象池获取节点(支持Prefab或Node)
  33. * @param prefab - Prefab资源或节点模板
  34. * @param parent - 可选父节点
  35. * @returns 可复用的节点实例
  36. */
  37. static getNode(prefab, parent) {
  38. var poolKey = prefab instanceof Prefab ? prefab.data.name : prefab.name;
  39. var node = null; //从对象池获取可用节点
  40. var pool = this._objectPools.get(poolKey);
  41. if ((pool == null ? void 0 : pool.length) > 0) {
  42. node = pool.pop();
  43. } else {
  44. node = instantiate(prefab);
  45. } //初始化节点状态
  46. if (node) {
  47. node.active = true;
  48. node.position = Vec3.ZERO;
  49. if (parent) {
  50. node.setParent(parent);
  51. }
  52. }
  53. return node;
  54. }
  55. /**
  56. * 通过名称从对象池获取节点
  57. * @param poolKey - 对象池键名
  58. * @param parent - 可选父节点
  59. * @returns 可复用的节点实例(如果没有则返回null)
  60. */
  61. static getName(poolKey, parent) {
  62. var pool = this._objectPools.get(poolKey);
  63. var node = pool == null ? void 0 : pool.pop();
  64. if (node) {
  65. node.active = true;
  66. if (parent) {
  67. node.setParent(parent);
  68. }
  69. }
  70. return node != null ? node : null;
  71. }
  72. /**
  73. * 将节点回收到对象池
  74. * @param node - 要回收的节点
  75. */
  76. static putNode(node) {
  77. if (!(node != null && node.isValid) || !node) return;
  78. var poolKey = node.name; //清理节点状态
  79. node.removeFromParent();
  80. node.active = false; //存入对象池
  81. if (poolKey) {
  82. if (!this._objectPools.has(poolKey)) {
  83. this._objectPools.set(poolKey, []);
  84. }
  85. this._objectPools.get(poolKey).push(node);
  86. } else {
  87. //没有有效名称的节点直接销毁
  88. node.destroy();
  89. }
  90. }
  91. /**
  92. * 获取指定对象池的大小
  93. * @param poolKey - 对象池键名
  94. * @returns 对象池中可用节点的数量
  95. */
  96. static getPoolSize(poolKey) {
  97. var _this$_objectPools$ge, _this$_objectPools$ge2;
  98. return (_this$_objectPools$ge = (_this$_objectPools$ge2 = this._objectPools.get(poolKey)) == null ? void 0 : _this$_objectPools$ge2.length) != null ? _this$_objectPools$ge : 0;
  99. }
  100. /**
  101. * 清空指定对象池
  102. * @param poolKey - 要清空的对象池键名
  103. */
  104. static clearPool(poolKey) {
  105. var pool = this._objectPools.get(poolKey);
  106. if (pool) {
  107. pool.forEach(node => node.destroy());
  108. this._objectPools.delete(poolKey);
  109. }
  110. }
  111. /**
  112. * 清空所有对象池
  113. */
  114. static clearAll() {
  115. this._objectPools.forEach((pool, key) => {
  116. pool.forEach(node => node.destroy());
  117. });
  118. this._objectPools.clear();
  119. }
  120. }, _class2._objectPools = new Map(), _class2)) || _class));
  121. _cclegacy._RF.pop();
  122. _crd = false;
  123. }
  124. };
  125. });
  126. //# sourceMappingURL=a61bc3ba3fb43f9bad46eb552a7a4a7650b1f404.js.map