I just encountered a bug. The large scene image needs to be loaded in advance, and all scene components need to wait for the image to be loaded before processing. However, since the scene background image is loaded asynchronously, and it is loaded asynchronously in a for loop at the same time, it is impossible to confirm which A picture was loaded at the end. With the mentality of giving it a try, I wrote it like this, but found it useless:
---记载所有背景图
function LoadBgRawImage(mapID,func)
for i = 1, len do
...
local path = "xxx\xxx\xxx.jpg"
local bgFunc = nil
if i == len then
bgFunc = func
end
---异步加载图片
LoadRawImage(path,func)
end
end
---异步加载图片
function LoadRawImage(path,func)
...(保护代码)
IOSystem.LoadAssetAsync(path,function(Prefab))
...
if func then ---执行回调
func()
end
end)
end
Then I asked happy xx (colleague) for help and received enlightening guidance:
---记载所有背景图
function LoadBgRawImage(mapID,func)
...
local loadedcount = len
for i = 1, len do
...
---异步加载图片
LoadRawImage(path,function()
loadedcount = loadedcount - 1
if loadedcount<=0 then
if func then
func()
end
end
end)
end
end
---异步加载图片
function LoadRawImage(path,func)
...(保护代码)
IOSystem.LoadAssetAsync(path,function(Prefab))
...
if func then ---执行回调
func()
end
end)
end
In this way, the callback can be executed after all loading is completed. Why?
Because the second way of writing is equivalent to registering a callback for each asynchronous load, and this callback can monitor the progress of each and perform summary processing.
To sum up: If you need to process callbacks asynchronously, you need to process callbacks inside asynchronously, not outside the callbacks.