rp_10012/assets/Loading/scripts/manager/NodePoolManager.ts
2025-07-23 14:21:38 +08:00

67 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}