123 lines
3.5 KiB
Lua
123 lines
3.5 KiB
Lua
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
|
|
|
|
function ImageLoad.SaveToGallery(url, album, filename, callback)
|
|
local asset = imgAssetMap[url]
|
|
if not asset or not asset.load or not asset.ntexture then
|
|
if callback then callback(false, "NOT_LOADED") end
|
|
return
|
|
end
|
|
|
|
local ntex = asset.ntexture
|
|
local unityTex = ntex.nativeTexture -- UnityEngine.Texture
|
|
|
|
if unityTex == nil then
|
|
if callback then callback(false, "NO_NATIVE_TEXTURE") end
|
|
return
|
|
end
|
|
|
|
local tex2d = ntex.nativeTexture
|
|
|
|
if tex2d == nil then
|
|
if callback then callback(false, "NO_TEXTURE") end
|
|
return
|
|
end
|
|
|
|
if not album then album = "UnityTest" end
|
|
if not filename then filename = "img_" .. os.time() .. ".png" end
|
|
|
|
GameApplication.Instance:SaveTextureToGallery(tex2d, album, filename, function(ok, path)
|
|
if callback then
|
|
callback(ok, path)
|
|
end
|
|
end)
|
|
end
|