12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import { Node,NodePool, Prefab, instantiate} from "cc";
- /** node对象池 */
- class PrefabPool {
- private _pools:Map<string,NodePool>= new Map();
- private _node:Node;
- constructor(){
- if(this._node==null){
- this._node=new Node();
- this._node.active=false;
- }
- }
- private getPool(path:string):NodePool{
- let pool= this._pools.get(path);
- if(pool==null){
- pool= new NodePool();
- this._pools.set(path,pool);
- }
- return pool;
- }
- public GetNode(p:Prefab):Node{
- let pool= this.getPool(p.name);
- let node = pool.get();
- if (!node){
- node = instantiate (p);
- node.name=p.name;
- }
- return node;
- }
- public RecNode(node:Node):void{
- node.parent=this._node;
- this.getPool(node.name).put(node);
- }
- public InitPool(p:Prefab,n:number=10):void{
- let pool= this.getPool(p.name);
- for(let i=0;i<n;i++){
- let node = instantiate (p);
- node.name=p.name;
- node.parent=this._node;
- pool.put(node);
- }
- }
- public Clear():void{
- if(!this._node)return;
- this._node.removeAllChildren();
- this._pools.forEach((v,k)=>{
- v.clear();
- })
- this._pools.clear();
- }
- public getAllPoolNames(): string[] {
- return Object.keys(this._pools);
- }
- public getPoolSize(poolName: string): number {
- const pool = this._pools[poolName];
- if (pool) {
- return pool.size();
- }
- return 0;
- }
- }
- export default function get_new_prefab_pool():PrefabPool { return new PrefabPool();}
|