是的我又在造轮子了 为什么要造轮子 因为我找不到现成的 那没办法了 只能硬着头皮造了 造起来吧董小姐
对象池主代码
require("Util/FunctionsEx")
require("Util/Queue")
require("Util/List")
LuaPool = class("LuaPool")
function LuaPool:ctor(prefab, count, position, parent, rotation)
-- 存放未使用的go
self.queue = Queue.new()
-- 存放已使用的go
self.list = List.new()
self.count = count
self.prefab = prefab
self.parent = parent
self.position = position
self.rotation = rotation
end
function LuaPool:Prewarm()
for i = 1, self.count do
local go = LoadTest(self.prefab, self.position, self.parent)
go.transform.name = i
self.list:Add(go)
self:Put(go)
end
end
function LuaPool:Put(go)
-- print("回收是否为真", self.list:Contains(go))
if self.list:Contains(go) == true then
self.list:Remove(go)
go:SetActive(false)
self.queue:Enqueue(go)
else
print("对象池回收出错")
end
end
function LuaPool:Get()
local go = nil
if self.queue:Count() == 0 then
go = LoadTest(self.prefab, self.position,self.parent)
else
go = self.queue:Dequeue()
end
self.list:Add(go)
go:SetActive(true)
return go
end
再把list和queue的发了吧 省得又是找到半个
require("Util/FunctionsEx")
List = class("List")
function List:ctor()
self.items = { }
end
function List:Add(item)
table.insert(self.items, self:Count() + 1, item)
end
function List:Count()
return #self.items
end
function List:Contains(item)
local isExist = false
for k, v in pairs(self.items) do
-- print("contains", item:GetInstanceID(), v:GetInstanceID())
if v:GetInstanceID() == item:GetInstanceID() then
isExist = true
break
end
end
return isExist
end
function List:Remove(item)
if not self:Contains(item) then return false end
for i = 1, self:Count() do
if self.items[i]:GetInstanceID() == item:GetInstanceID() then
table.remove(self.items, i)
break
end
end
end
function List:RemoveAt(index)
if not self:Contains(self.items[index]) then return false end
table.remove(self.items, index)
end
require("Util/FunctionsEx")
Queue = class("Queue")
function Queue:ctor()
self.items = { }
end
function Queue:Peek()
return self.items[1]
end
function Queue:Enqueue(item)
table.insert(self.items, self:Count() + 1, item)
end
function Queue:Dequeue()
local item = self:Peek()
table.remove(self.items, 1)
return item
end
function Queue:Count()
return #self.items
end
function Queue:Clear()
self.items = { }
end
先把要初始的prefab地址 初始的数目传进来 具体设置自己定
然后就测试对象池 的get和put方法了
ok今天到这 最近估计要研究pbc这块了 如果有些感悟就写点这方面的东西 下次见