ResMgr.ts 3.6 KB

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