12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import { _decorator, Component, Node, Prefab } from 'cc';
- import { GameObjectPool } from './GameObjectPool';
- export class PoolManager {
- private static _instance: PoolManager;
- private _pools: Map<string, GameObjectPool> = new Map();
- public static get instance(){
- if(!this._instance){
- this._instance = new PoolManager();
- }
- return this._instance;
- }
- public registerPool(prefab: Prefab, initialSize: number = 10){
- const key = prefab.name;
- if(!this._pools.has(key)){
- this._pools.set(key, new GameObjectPool(prefab, initialSize));
- }
- }
- //获取节点实例
- public get(prefab: Prefab): Node{
- const key = prefab.name;
- if(!this._pools.has(key)){
- this.registerPool(prefab);
- }
- return this._pools.get(key)!.get();
- }
- //回收节点
- public put(node: Node){
- const key = node.name;
- if(this._pools.has(key)){
- this._pools.get(key)!.put(node);
- } else {
- Error("PoolManager destroy")
- node.destroy();
- }
- }
- }
|