对象池管理类
import { NodePool } from "./NodePool"; const { ccclass, property } = cc._decorator; export var PoolData = cc.Class({ name: 'PoolData', properties: { /** 对象名,根据这个获取对象 */ name: 'obj', /** 对象预制体 */ nodePrefab:cc.Prefab, //对象初始化最大数量 maxCount:10, } }); /** * @name JoeyHuang * @date 2020/7/1 13:49:22 * @description 描述 多对象的对象池管理,根据对象名字去获取相应对象 */ @ccclass export default class PoolManager extends cc.Component { public static Instance: PoolManager; //这里设置多少个对象 @property(PoolData) poolDataLists:any=[]; @property maxCount:number=30; private poolMap: Map<string, NodePool> = new Map(); onLoad() { PoolManager.Instance = this; this.Initialize(); } Initialize() { for (let i = 0; i < this.nodePrefabs.length; i++) { this.poolMap.set(this.poolDataLists[i].name, new NodePool(this.poolDataLists[i].nodePrefab, this.poolDataLists[i].maxCount)); } } /** * 根据名字拿取对象 * @param poolName 对象名字 */ public Spawn(poolName:string):cc.Node { if(this.poolMap.has(poolName)) { return this.poolMap.get(poolName).Spawn(); } return null; } /** * 回收对象 * @param poolName 对象名 * @param _node 对象实例 */ public UnSpawn(poolName:string,_node:cc.Node) { if(this.poolMap.has(poolName)) { this.poolMap.get(poolName).UnSpawn(_node); } } }需要把PoolManager挂在一个节点上,空节点就行。给每个对象设置好名字,赋值预制体以及初始化数量,我们拿取对象或者回收对象的时候都需要用到对象名,如图: