miduo_client/Assets/ManagedResources/~Lua/Modules/Common/SpriteLoader.lua

70 lines
2.0 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

local SpriteLoaderPool = {}
local SpriteLoader = {}
local spLoaderIndex = 0
local IsActive = ServerConfigManager.IsSettingActive(ServerConfigManager.SettingConfig.SPRITE_LOADER)
-- 调用此方法创建一个管理器
function SpriteLoader.New()
-- -- 判断对象池里有没有
-- if #SpriteLoaderPool > 0 then
-- return table.remove(SpriteLoaderPool, 1)
-- end
-- 没有新建
spLoaderIndex = spLoaderIndex + 1
local o = {}
o.SpriteList = {}
o.id = spLoaderIndex
SpriteLoader.__index = SpriteLoader
setmetatable(o, SpriteLoader)
return o
end
function SpriteLoader:LoadSprite(name)
if not IsActive then
return Util.LoadSprite(name)
end
local transName = GetTranslateSpriteName(name)
local sp = resMgr:LoadSpriteAsset(transName)
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()
if not SpriteLoaderPool[self.id] then
SpriteLoaderPool[self.id] = 0
end
SpriteLoaderPool[self.id] = SpriteLoaderPool[self.id] + 1
if SpriteLoaderPool[self.id] > 1 then
--LogError("SpriteLoader Repeat UnLoad : ".. self.id..", count : "..SpriteLoaderPool[self.id])
end
for name, count in pairs(self.SpriteList) do
-- if name == "shadow_4" then
-- LogWarn("lua 回收资源name:"..name..", count:"..count)
-- end
resMgr:UnLoadAsset(name, count)
end
self.SpriteList = {}
-- -- 回收到对象池
-- table.insert(SpriteLoaderPool, self)
end
-- 销毁loader
function SpriteLoader:Destroy()
if not IsActive then
return
end
coroutine.start(function()
coroutine.wait(2)
self:UnLoadSprite()
end)
end
return SpriteLoader