89 lines
2.3 KiB
Lua
89 lines
2.3 KiB
Lua
ImageLoad = {}
|
|
|
|
local imgAssetMap = {}
|
|
local imgQueue = Queue.new(2000)
|
|
|
|
local function DownLoadImg(url, icon)
|
|
local www = UnityEngine.WWW(url)
|
|
coroutine.www(www)
|
|
if string.utf8len(www.error) == 0 then
|
|
local obj = imgAssetMap[url]
|
|
if obj and not obj.load then
|
|
local texture = www.texture
|
|
-- www:Dispose()
|
|
if (texture ~= null) then
|
|
-- local ntexture = FairyGUI.NTexture(texture)
|
|
obj.ntexture = texture
|
|
obj.load = true
|
|
obj.co = nil
|
|
icon.texture = texture
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
|
|
local function SetTexture()
|
|
if (imgQueue:Count() > 0) then
|
|
local tem = imgQueue:Dequeue()
|
|
local obj = imgAssetMap[tem.url]
|
|
if not tem._iconObject and obj then
|
|
if obj.load then
|
|
tem._iconObject.texture = obj.ntexture
|
|
if tem.callback then
|
|
tem.callback()
|
|
end
|
|
else
|
|
imgQueue:Enqueue(tem)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
UpdateBeat:Add(SetTexture)
|
|
|
|
-- group 图片分组
|
|
function ImageLoad.Load(url, _iconObject, group, callback)
|
|
if string.utf8len(url) == 0 then
|
|
return
|
|
end
|
|
if not group then
|
|
group = "common"
|
|
end
|
|
|
|
|
|
local asset = imgAssetMap[url]
|
|
|
|
if (asset ~= nil) then
|
|
if asset.load then
|
|
_iconObject.texture = asset.ntexture
|
|
if callback then callback() end
|
|
else
|
|
imgQueue:Enqueue({ url = url, _iconObject = _iconObject, callback = callback })
|
|
end
|
|
return
|
|
end
|
|
|
|
local _co = coroutine.start(DownLoadImg, url, _iconObject)
|
|
-- local _co_load = coroutine.start(SetTexture,_iconObject,url,callback)
|
|
imgAssetMap[url] = { group = group, load = false, co = _co }
|
|
imgQueue:Enqueue({ url = url, _iconObject = _iconObject, callback = callback })
|
|
end
|
|
|
|
function ImageLoad.Clear(group)
|
|
for i, v in pairs(imgAssetMap) do
|
|
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
|
|
end
|