poolManager.ts 3.9 KB

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