2021-04-21 13:12:04 +08:00
|
|
|
|
local SpriteLoaderPool = {}
|
|
|
|
|
local SpriteLoader = {}
|
2021-04-22 11:20:16 +08:00
|
|
|
|
local spLoaderIndex = 0
|
2021-05-28 14:34:01 +08:00
|
|
|
|
local IsActive = ServerConfigManager.IsSettingActive(ServerConfigManager.SettingConfig.SPRITE_LOADER)
|
2021-04-21 13:12:04 +08:00
|
|
|
|
|
|
|
|
|
-- 调用此方法创建一个管理器
|
|
|
|
|
function SpriteLoader.New()
|
2021-04-21 22:45:37 +08:00
|
|
|
|
-- -- 判断对象池里有没有
|
|
|
|
|
-- if #SpriteLoaderPool > 0 then
|
|
|
|
|
-- return table.remove(SpriteLoaderPool, 1)
|
|
|
|
|
-- end
|
2021-04-21 13:12:04 +08:00
|
|
|
|
-- 没有新建
|
2021-04-22 11:20:16 +08:00
|
|
|
|
spLoaderIndex = spLoaderIndex + 1
|
2021-04-21 13:12:04 +08:00
|
|
|
|
local o = {}
|
|
|
|
|
o.SpriteList = {}
|
2021-04-22 11:20:16 +08:00
|
|
|
|
o.id = spLoaderIndex
|
2021-04-21 13:12:04 +08:00
|
|
|
|
SpriteLoader.__index = SpriteLoader
|
|
|
|
|
setmetatable(o, SpriteLoader)
|
|
|
|
|
return o
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function SpriteLoader:LoadSprite(name)
|
2021-05-28 14:34:01 +08:00
|
|
|
|
if not IsActive then
|
|
|
|
|
return Util.LoadSprite(name)
|
|
|
|
|
end
|
2021-04-21 13:12:04 +08:00
|
|
|
|
local transName = GetTranslateSpriteName(name)
|
2021-05-19 17:26:50 +08:00
|
|
|
|
local sp = resMgr:LoadSpriteAsset(transName)
|
2021-04-21 13:12:04 +08:00
|
|
|
|
if sp then
|
|
|
|
|
if not self.SpriteList[sp.name] then
|
|
|
|
|
self.SpriteList[sp.name] = 0
|
|
|
|
|
end
|
|
|
|
|
-- 每次加载会导致引用计数加2
|
|
|
|
|
self.SpriteList[sp.name] = self.SpriteList[sp.name] + 1
|
|
|
|
|
return sp
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- 根据界面加载的数量卸载相应数量的资源
|
|
|
|
|
function SpriteLoader:UnLoadSprite()
|
2021-04-22 11:20:16 +08:00
|
|
|
|
if not SpriteLoaderPool[self.id] then
|
|
|
|
|
SpriteLoaderPool[self.id] = 0
|
|
|
|
|
end
|
|
|
|
|
SpriteLoaderPool[self.id] = SpriteLoaderPool[self.id] + 1
|
|
|
|
|
if SpriteLoaderPool[self.id] > 1 then
|
2021-05-27 20:24:33 +08:00
|
|
|
|
--LogError("SpriteLoader Repeat UnLoad : ".. self.id..", count : "..SpriteLoaderPool[self.id])
|
2021-04-22 11:20:16 +08:00
|
|
|
|
end
|
2021-04-21 13:12:04 +08:00
|
|
|
|
for name, count in pairs(self.SpriteList) do
|
2021-04-23 14:08:27 +08:00
|
|
|
|
-- if name == "shadow_4" then
|
|
|
|
|
-- LogWarn("lua 回收资源:name:"..name..", count:"..count)
|
|
|
|
|
-- end
|
2021-04-21 13:12:04 +08:00
|
|
|
|
resMgr:UnLoadAsset(name, count)
|
|
|
|
|
end
|
|
|
|
|
self.SpriteList = {}
|
2021-04-22 11:20:16 +08:00
|
|
|
|
-- -- 回收到对象池
|
|
|
|
|
-- table.insert(SpriteLoaderPool, self)
|
2021-04-21 13:12:04 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- 销毁loader
|
|
|
|
|
function SpriteLoader:Destroy()
|
2021-05-28 14:34:01 +08:00
|
|
|
|
if not IsActive then
|
|
|
|
|
return
|
|
|
|
|
end
|
2021-04-22 14:37:16 +08:00
|
|
|
|
coroutine.start(function()
|
|
|
|
|
coroutine.wait(2)
|
|
|
|
|
self:UnLoadSprite()
|
|
|
|
|
end)
|
2021-04-21 13:12:04 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return SpriteLoader
|