| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import { instantiate, Node, NodePool, Prefab, Vec3 } from "cc"
- /**
- * @Description: resources manager and pool manager
- * @return {*}
- */
- export class CachePool {
-
- private _dictPool: { [key: string]: NodePool } = {}
- private _dictPrefab: { [key: string]: Prefab } = {}
- private _loadStemp = null;
- private _loadTime = 0;
- private _totalTime = 0
- public loadingRate = 0;
- public debug = false;
-
- private static _inst: CachePool;
- public static get inst(): CachePool {
- if (this._inst == null) {
- this._inst = new CachePool();
- }
- return this._inst;
- }
- /**
- * @description: get the node from the pool
- * @param {Prefab} prefab
- * @param {Node} parent
- * @param {Vec3} pos
- * @return {*}
- */
- // 节点池 NodePool ,用于处理频繁创建和销毁的物体
- public getNode (prefab: Prefab | string, parent?: Node, pos?: Vec3): Node {
- let tempPre;
- let name;
- if (typeof prefab === 'string') {
- tempPre = this._dictPrefab[prefab];
- name = prefab;
- if (!tempPre) {
- // console.log("Pool invalid prefab name = ", name);
- return null;
- }
- }
- else {
- tempPre = prefab;
- name = prefab.data.name;
- }
- let node = null;
- if (this._dictPool.hasOwnProperty(name)) {
- //own this pool
- let pool = this._dictPool[name];
- if (pool.size() > 0) {
- node = pool.get();
- } else {
- node = instantiate(tempPre);
- }
- } else {
- //create new pool
- let pool = new NodePool();
- this._dictPool[name] = pool;
- node = instantiate(tempPre);
- }
- if (parent) {
- node.parent = parent;
- node.active = true;
- if (pos) node.position = pos;
- }
- return node;
- }
- /**
- * @description: put the node into the pool
- * @param {Node} node
- * @param {*} isActive
- * @return {*}
- */
- public putNode (node: Node | null, isActive = false) {
- if (!node) {
- return;
- }
- let name = node.name;
- let pool = null;
- // node.active = isActive
- if (this._dictPool.hasOwnProperty(name)) {
- pool = this._dictPool[name];
- } else {
- pool = new NodePool();
- this._dictPool[name] = pool;
- }
- pool.put(node);
- }
- /**
- * @description: clear the pool based on name
- * @param {string} name
- * @return {*}
- */
- public clearPool (name: string) {
- if (this._dictPool.hasOwnProperty(name)) {
- let pool = this._dictPool[name];
- pool.clear();
- }
- }
- public setPrefab (name: string, prefab: Prefab): void {
- if (!this._dictPrefab[name]) {
- this._dictPrefab[name] = prefab;
- }
- }
- public clearDict () {
- this._dictPrefab = {};
- }
- printTimer (name: string = "", end = false) {
- this._loadTime = Date.now() - this._loadStemp;
- this._loadStemp = Date.now();
- this._totalTime += this._loadTime
- console.log(name + ",load time===", this._loadTime, "ms")
- if (end) {
- console.log("Load finish, total time===", this._totalTime, "ms")
- }
- }
- }
|