require("Base/Stack") BATTLE_POOL_TYPE = { MY_ROLE = 0, ENEMY_ROLE = 1, ENEMY_ROLE_2 = 2, BUFF_VIEW = 3, Monster = 4, } BattlePool = {} local this = BattlePool this.pool = {} this.momItems = {} function this.Init(poolRoot) this.root = poolRoot this.momItems = {} end function this.Register(type, item) if this.momItems[type] then LogRed("战斗对象池中重复注册了节点, type = "..type) return end this.momItems[type] = item end -- 获取一个节点 function this.GetItem(parent, type) if not this.momItems[type] then LogRed("该类型节点尚未在对象池中注册, type = "..type) return end if not this.pool[type] then this.pool[type] = Stack.New() end -- 判断是否存在 local item = this.pool[type]:Peek() if not item then item = newObject(this.momItems[type]) else item = this.pool[type]:Pop() end -- item.transform:SetParent(parent.transform) return item end -- 回收一个节点 function this.RecycleItem(item, type) if not this.momItems[type] then LogRed("该类型节点尚未在对象池中注册, type = "..type) return end if not this.pool[type] then this.pool[type] = Stack.New() end -- item.transform:SetParent(this.root.transform) this.pool[type]:Push(item) end -- 清空对象池 function this.Clear() for _, st in pairs(this.pool) do st:Foreach(function(item) destroy(item) end) end this.pool = {} this.momItems = {} end function this.Clear2() for _, st in pairs(this.pool) do st:Foreach(function(item) destroy(item) end) end this.pool = {} --this.momItems = {} end -- 销毁对象池 -- function this.Destroy() -- this.Clear() -- end return BattlePool