import { instantiate, Prefab ,Node, isValid} from "cc"; export default class NodePools{ list:Node[]=[]; private _insNode:Node | Prefab; constructor(insNode:Node | Prefab,preCount:number = 8){ this._insNode = insNode; for (let i = 0; i < preCount; i++) { this.list.push(instantiate(insNode) as Node); } } get(parent:Node){ let tNode:Node = null; if(this.list.length>0){ tNode = this.list.pop(); }else{ tNode = instantiate(this._insNode) as Node; } if(tNode.parent != parent){tNode.setParent(parent);} return tNode; } put(tNode:Node){ tNode.removeFromParent(); this.list.push(tNode); } clear(){ for (let i = 0; i < this.list.length; i++) { const tNode = this.list[i]; if(isValid(tNode)){ tNode.destroy(); } } this.list.length = 0; } }