--UI对象池
|
|
UIObjPool = UIObjPool or BaseClass()
|
|
local UIObjPool = UIObjPool
|
|
|
|
--类型ID,从1开始递增
|
|
UIObjPool.UIType = {
|
|
AwardItem = 1,
|
|
}
|
|
--缓存对象信息
|
|
UIObjPool.UIInfo = {
|
|
[UIObjPool.UIType.AwardItem] = {id = 1, max_num = 15, class_type = "AwardItem"},
|
|
}
|
|
|
|
function UIObjPool:__init()
|
|
UIObjPool.Instance = self
|
|
self.is_init = false
|
|
self.ui_pool = {}
|
|
for k,v in pairs(UIObjPool.UIType) do
|
|
self.ui_pool[v] = Array.New()
|
|
end
|
|
|
|
local pool_cont = GameObject.Find("UIObjPoolContainer")
|
|
if not IsNull(pool_cont) then
|
|
self.parent_cont = pool_cont.transform
|
|
self.parent_cont.gameObject:SetActive(false)
|
|
else
|
|
self.parent_cont = panelMgr:GetParent("UI")
|
|
end
|
|
|
|
self:InitUIPool()
|
|
end
|
|
|
|
function UIObjPool:getInstance()
|
|
if UIObjPool.Instance == nil then
|
|
UIObjPool.New()
|
|
end
|
|
return UIObjPool.Instance
|
|
end
|
|
|
|
function UIObjPool:__delete( )
|
|
self:CancelStepTimer()
|
|
for i=1, TableSize(UIObjPool.UIType) do
|
|
local pool = self.ui_pool[i]
|
|
for k,v in pairs(pool) do
|
|
v:DeleteMe()
|
|
end
|
|
end
|
|
self.ui_pool = {}
|
|
end
|
|
|
|
function UIObjPool:InitUIPool( )
|
|
--避免重复执行
|
|
if self.is_init then
|
|
return
|
|
end
|
|
self.is_init = true
|
|
|
|
--分步初始化对象池
|
|
self.pool_index, self.info_index = 1, 1
|
|
local pool_len = TableSize(UIObjPool.UIType)
|
|
local item
|
|
|
|
local function on_init( )
|
|
if self.pool_index > pool_len then
|
|
self:CancelStepTimer()
|
|
return
|
|
end
|
|
local info = UIObjPool.UIInfo[self.pool_index]
|
|
if not info then
|
|
self:CancelStepTimer()
|
|
return
|
|
end
|
|
if self.info_index > info.max_num then
|
|
self.pool_index = self.pool_index + 1
|
|
else
|
|
item = _G[info.class_type].New(self.parent_cont)
|
|
item:SetVisible(false)
|
|
self.ui_pool[self.pool_index]:PushFront(item)
|
|
self.info_index = self.info_index + 1
|
|
end
|
|
-- print("huangcong:UIObjPool [start:68] TableSize():", TableSize(self.ui_pool))
|
|
end
|
|
|
|
if not self.step_init_id then
|
|
self.step_init_id = GlobalTimerQuest:AddPeriodQuest(on_init, 0.02, -1)
|
|
end
|
|
end
|
|
|
|
function UIObjPool:CancelStepTimer( )
|
|
if self.step_init_id then
|
|
GlobalTimerQuest:CancelQuest(self.step_init_id)
|
|
self.step_init_id = nil
|
|
end
|
|
end
|
|
|
|
--外部接口,从对象池获取对象(类型id, 对象的父节点)
|
|
function UIObjPool:PopItem( ui_type, ui_parent)
|
|
local pool = self.ui_pool[ui_type]
|
|
local info = UIObjPool.UIInfo[ui_type]
|
|
local item
|
|
if pool and pool:GetSize() > 0 then
|
|
item = pool:PopFront()
|
|
if ui_parent then
|
|
item:SetVisible(true)
|
|
item.parent = ui_parent
|
|
item.transform:SetParent(ui_parent)
|
|
item:SetPosition(0,0)
|
|
end
|
|
if item.ResetInfo then
|
|
item:ResetInfo()
|
|
end
|
|
else
|
|
if info and ui_parent then
|
|
item = _G[info.class_type].New(ui_parent)
|
|
end
|
|
end
|
|
return item
|
|
end
|
|
|
|
--外部接口,存入对象池(类型id, ui对象)
|
|
function UIObjPool:PushItem( ui_type, ui_item )
|
|
-------------------------
|
|
if ui_type == UIObjPool.UIType.AwardItem then
|
|
if not ui_item.SetItemSize then
|
|
GlobalEventSystem:Fire(LuaErrorModel.SEND_LUAERROR_MESSAGE,"存了一个奇怪的东西进AwardItem对象池,请把日志移交前端")
|
|
print('---- UIObjPool.lua -- 存了一个奇怪的东西进AwardItem对象池')
|
|
PrintCallStack()
|
|
end
|
|
end
|
|
-------------------------
|
|
local info = UIObjPool.UIInfo[ui_type]
|
|
local pool = self.ui_pool[ui_type]
|
|
if (ui_item and ui_item.is_loaded) and pool:GetSize() < info.max_num then
|
|
ui_item.transform:SetParent(self.parent_cont)
|
|
ui_item:SetVisible(false)
|
|
pool:PushFront(ui_item)
|
|
else
|
|
if ui_item then
|
|
ui_item:DeleteMe()
|
|
ui_item = nil
|
|
end
|
|
end
|
|
end
|