CachePool.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { instantiate, Node, NodePool, Prefab, Vec3 } from "cc"
  2. /**
  3. * @Description: resources manager and pool manager
  4. * @return {*}
  5. */
  6. export class CachePool {
  7. private _dictPool: { [key: string]: NodePool } = {}
  8. private _dictPrefab: { [key: string]: Prefab } = {}
  9. private _loadStemp = null;
  10. private _loadTime = 0;
  11. private _totalTime = 0
  12. public loadingRate = 0;
  13. public debug = false;
  14. private static _inst: CachePool;
  15. public static get inst(): CachePool {
  16. if (this._inst == null) {
  17. this._inst = new CachePool();
  18. }
  19. return this._inst;
  20. }
  21. /**
  22. * @description: get the node from the pool
  23. * @param {Prefab} prefab
  24. * @param {Node} parent
  25. * @param {Vec3} pos
  26. * @return {*}
  27. */
  28. // 节点池 NodePool ,用于处理频繁创建和销毁的物体
  29. public getNode (prefab: Prefab | string, parent?: Node, pos?: Vec3): Node {
  30. let tempPre;
  31. let name;
  32. if (typeof prefab === 'string') {
  33. tempPre = this._dictPrefab[prefab];
  34. name = prefab;
  35. if (!tempPre) {
  36. // console.log("Pool invalid prefab name = ", name);
  37. return null;
  38. }
  39. }
  40. else {
  41. tempPre = prefab;
  42. name = prefab.data.name;
  43. }
  44. let node = null;
  45. if (this._dictPool.hasOwnProperty(name)) {
  46. //own this pool
  47. let pool = this._dictPool[name];
  48. if (pool.size() > 0) {
  49. node = pool.get();
  50. } else {
  51. node = instantiate(tempPre);
  52. }
  53. } else {
  54. //create new pool
  55. let pool = new NodePool();
  56. this._dictPool[name] = pool;
  57. node = instantiate(tempPre);
  58. }
  59. if (parent) {
  60. node.parent = parent;
  61. node.active = true;
  62. if (pos) node.position = pos;
  63. }
  64. return node;
  65. }
  66. /**
  67. * @description: put the node into the pool
  68. * @param {Node} node
  69. * @param {*} isActive
  70. * @return {*}
  71. */
  72. public putNode (node: Node | null, isActive = false) {
  73. if (!node) {
  74. return;
  75. }
  76. let name = node.name;
  77. let pool = null;
  78. // node.active = isActive
  79. if (this._dictPool.hasOwnProperty(name)) {
  80. pool = this._dictPool[name];
  81. } else {
  82. pool = new NodePool();
  83. this._dictPool[name] = pool;
  84. }
  85. pool.put(node);
  86. }
  87. /**
  88. * @description: clear the pool based on name
  89. * @param {string} name
  90. * @return {*}
  91. */
  92. public clearPool (name: string) {
  93. if (this._dictPool.hasOwnProperty(name)) {
  94. let pool = this._dictPool[name];
  95. pool.clear();
  96. }
  97. }
  98. public setPrefab (name: string, prefab: Prefab): void {
  99. if (!this._dictPrefab[name]) {
  100. this._dictPrefab[name] = prefab;
  101. }
  102. }
  103. public clearDict () {
  104. this._dictPrefab = {};
  105. }
  106. printTimer (name: string = "", end = false) {
  107. this._loadTime = Date.now() - this._loadStemp;
  108. this._loadStemp = Date.now();
  109. this._totalTime += this._loadTime
  110. console.log(name + ",load time===", this._loadTime, "ms")
  111. if (end) {
  112. console.log("Load finish, total time===", this._totalTime, "ms")
  113. }
  114. }
  115. }