ImageLoad = {} local imgAssetMap = {} local imgQueue = Queue.new(2000) local function DownLoadImg(url) 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 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.isDisposed and obj then if obj.load then tem._iconObject.texture = obj.ntexture if tem.callback then tem.callback() end else local _co = coroutine.start(DownLoadImg, tem.url) -- local _co_load = coroutine.start(SetTexture,_iconObject,url,callback) tem.co = _co 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) -- 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