You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
980 B
41 lines
980 B
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;
|
|
}
|
|
|
|
|
|
}
|