67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import { _decorator, instantiate, Node, NodePool, Prefab } from 'cc';
|
||
import { ResManager } from './ResManager';
|
||
const { ccclass, property } = _decorator;
|
||
|
||
@ccclass('NodePoolManager')
|
||
export class NodePoolManager {
|
||
private static _instance: NodePoolManager = null;
|
||
private _nodePoolMap: Map<string, NodePool> = null;
|
||
|
||
static get instance() {
|
||
if (this._instance) {
|
||
return this._instance;
|
||
}
|
||
this._instance = new NodePoolManager();
|
||
return this._instance;
|
||
}
|
||
|
||
constructor() {
|
||
this._nodePoolMap = new Map<string, NodePool>();
|
||
}
|
||
|
||
// 内部调用,通过name获取节点池
|
||
getNodePoolByName(name: string): NodePool {
|
||
if (!this._nodePoolMap.has(name)) {
|
||
let nodePool = new NodePool(name);
|
||
this._nodePoolMap.set(name, nodePool)
|
||
}
|
||
let nodePool = this._nodePoolMap.get(name);
|
||
return nodePool
|
||
}
|
||
|
||
getNodeFromPoolStatic(name: string, prefab?: Prefab): Node | null {
|
||
let nodePool = this.getNodePoolByName(name);
|
||
let poolSize = nodePool.size();
|
||
if (poolSize <= 0) {
|
||
let node = instantiate(prefab);
|
||
nodePool.put(node);
|
||
}
|
||
return nodePool.get();
|
||
}
|
||
|
||
putNodeToPool(name: string, node: Node) {
|
||
let nodePool = this.getNodePoolByName(name);
|
||
nodePool.put(node);
|
||
}
|
||
|
||
clearNodePoolByName(name: string) {
|
||
let nodePool = this.getNodePoolByName(name);
|
||
nodePool.clear();
|
||
if (this._nodePoolMap.has(name)) {
|
||
this._nodePoolMap.delete(name);
|
||
}
|
||
}
|
||
|
||
clearAll() {
|
||
this._nodePoolMap.forEach(value => {
|
||
value.clear();
|
||
})
|
||
this._nodePoolMap.clear();
|
||
}
|
||
|
||
static destoryInstance() {
|
||
this._instance = null;
|
||
}
|
||
}
|
||
|