poolManager.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { _decorator, Prefab, Node, instantiate, NodePool } from "cc";
  2. const { ccclass, property } = _decorator;
  3. @ccclass("PoolManager")
  4. export class PoolManager {
  5. private _dictPool: Map<string,NodePool> = new Map()//对象池字典
  6. private _dictPrefab: any = {}//预制体字典
  7. static _instance: PoolManager;
  8. static get instance() {
  9. if (this._instance) {
  10. return this._instance;
  11. }
  12. this._instance = new PoolManager();
  13. return this._instance;
  14. }
  15. public isHavePool(name: string):boolean{
  16. if(this._dictPrefab[name]){
  17. return true;
  18. }
  19. return false;
  20. }
  21. /**
  22. * 根据预设从对象池中获取对应节点
  23. */
  24. public getNode(prefab: Prefab, parent: Node) {
  25. let name = prefab.name;
  26. //@ts-ignore
  27. if (!prefab.position) {
  28. //@ts-ignore
  29. name = prefab.data.name;
  30. }
  31. this._dictPrefab[name] = prefab;
  32. let node: Node = null!;
  33. if (this._dictPool.get(name)) {
  34. //已有对应的对象池
  35. let pool = this._dictPool[name];
  36. if (pool.size() > 0) {
  37. console.log("is get pool?")
  38. node = pool.get();
  39. } else {
  40. node = instantiate(prefab);
  41. }
  42. } else {
  43. //没有对应对象池,创建他!
  44. let pool = new NodePool();
  45. this._dictPool[name] = pool;
  46. node = instantiate(prefab);
  47. }
  48. node.parent = parent;
  49. node.active = true;
  50. return node;
  51. }
  52. /**
  53. * 将对应节点放回对象池中
  54. */
  55. public putNode(name:string,node: Node) {
  56. if (!node) {
  57. return;
  58. }
  59. let pool = null;
  60. if (this._dictPool.get(name)) {
  61. //已有对应的对象池
  62. pool = this._dictPool[name];
  63. } else {
  64. //没有对应对象池,创建他!
  65. pool = new NodePool();
  66. this._dictPool[name] = pool;
  67. }
  68. pool.put(node);
  69. }
  70. /**
  71. * 根据名称,清除对应对象池
  72. */
  73. public clearPool(name: string) {
  74. if (this._dictPool.hasOwnProperty(name)) {
  75. let pool = this._dictPool[name];
  76. pool.clear();
  77. }
  78. }
  79. /**
  80. * 预生成对象池
  81. *
  82. * @param {Prefab} prefab 预制体
  83. * @param {number} num 需要预加载的数量
  84. * @returns
  85. * @memberof PoolManager
  86. */
  87. public preloadPool (prefab: Prefab, num: number) {
  88. let name = prefab.name;
  89. // @ts-ignore
  90. if (!prefab.position) {
  91. // @ts-ignore
  92. name = prefab.data.name;
  93. }
  94. let pool: any = null;
  95. if (this._dictPool.hasOwnProperty(name)) {
  96. // 已有对应的对象池
  97. pool = this._dictPool[name];
  98. } else {
  99. // 没有对应对象池,创建他!
  100. pool = new NodePool();
  101. this._dictPool[name] = pool;
  102. }
  103. for (let i = 0; i < num; i++) {
  104. let node = instantiate(prefab);
  105. pool.put(node);
  106. }
  107. }
  108. public getNodeByPoolName(name:string) {
  109. let node: Node = null!;
  110. let pool = this._dictPool.get(name);
  111. console.log("pool.size()",pool.size())
  112. if (pool.size() > 0) {
  113. console.log("is get pool?")
  114. node = pool.get();
  115. }
  116. return node;
  117. }
  118. public preloadPoolByName(name:string, num: number,prefab:Prefab) {
  119. let pool: NodePool = null;
  120. if (this._dictPool.get(name)) {
  121. // 已有对应的对象池
  122. pool = this._dictPool[name];
  123. } else {
  124. // 没有对应对象池,创建他!
  125. pool = new NodePool();
  126. this._dictPool.set(name,pool);
  127. }
  128. for (let i = 0; i < num; i++) {
  129. let node = instantiate(prefab);
  130. pool.put(node);
  131. }
  132. }
  133. }