hengyang_client/lua_probject/base_project/Game/View/Common/ImageLoad.lua

91 lines
2.6 KiB
Lua
Raw Normal View History

2025-04-01 10:48:36 +08:00
ImageLoad = {}
local imgAssetMap = {}
local imgQueue = Queue.new(2000)
local function DownLoadImg(url)
2025-09-17 19:51:05 +08:00
local www = UnityEngine.WWW(url)
coroutine.www(www)
if (www.error == nil or www.error == "") and www.bytesDownloaded > 0 then
local texture = www.texture
if texture ~= nil and texture.width > 2 and texture.height > 2 then
local obj = imgAssetMap[url]
if obj and not obj.load then
www:Dispose()
if (texture ~= null) then
local ntexture = FairyGUI.NTexture(texture)
obj.ntexture = ntexture
obj.load = true
obj.co = nil
end
2025-04-01 10:48:36 +08:00
end
end
2025-09-17 19:51:05 +08:00
end
2025-04-01 10:48:36 +08:00
end
local function SetTexture()
if (imgQueue:Count() > 0) then
local tem = imgQueue:Dequeue()
local obj = imgAssetMap[tem.url]
if not tem._iconObject.isDisposed and obj then
if obj.load then
tem._iconObject.texture = obj.ntexture
if tem.callback then
tem.callback()
end
else
2025-09-17 19:51:05 +08:00
local _co = coroutine.start(DownLoadImg, tem.url)
-- local _co_load = coroutine.start(SetTexture,_iconObject,url,callback)
tem.co = _co
2025-04-01 10:48:36 +08:00
imgQueue:Enqueue(tem)
end
end
end
end
UpdateBeat:Add(SetTexture)
-- group 图片分组
2025-09-17 19:51:05 +08:00
function ImageLoad.Load(url, _iconObject, group, callback)
if string.utf8len(url) == 0 then
return
end
2025-04-01 10:48:36 +08:00
if not group then
group = "common"
end
2025-09-17 19:51:05 +08:00
local asset = imgAssetMap[url]
2025-04-01 10:48:36 +08:00
if (asset ~= nil) then
if asset.load then
_iconObject.texture = asset.ntexture
if callback then callback() end
else
2025-09-17 19:51:05 +08:00
imgQueue:Enqueue({ url = url, _iconObject = _iconObject, callback = callback })
2025-04-01 10:48:36 +08:00
end
return
end
2025-09-17 19:51:05 +08:00
local _co = coroutine.start(DownLoadImg, url)
2025-04-01 10:48:36 +08:00
-- local _co_load = coroutine.start(SetTexture,_iconObject,url,callback)
2025-09-17 19:51:05 +08:00
imgAssetMap[url] = { group = group, load = false, co = _co }
imgQueue:Enqueue({ url = url, _iconObject = _iconObject, callback = callback })
end
2025-04-01 10:48:36 +08:00
function ImageLoad.Clear(group)
2025-09-17 19:51:05 +08:00
for i, v in pairs(imgAssetMap) do
2025-04-01 10:48:36 +08:00
if v.group == group then
if v.co then
coroutine.stop(v.co)
end
-- if v.co_load then
-- coroutine.stop(v.co_load)
-- end
if v.load then
imgAssetMap[i].ntexture:Unload(true)
end
imgAssetMap[i] = nil
end
end
2025-09-17 19:51:05 +08:00
end