- Home /
LoadAsync is blocking?
I'm trying to prevent my game freezing up while loading assets in a new scene, so in the Start method, I have a for loop that calls Resources.LoadAsync a bunch of times and saves the returned ResourceRequest to a list. Then in my Update method, I run through the list to see what's finished loading and assign the asset as needed. So it's like
void Start()
{
for(int i = 0; i < n; i++)
{
mylist.Add(Resources.LoadAsync(path));
}
}
void Update()
{
for(int i = 0; i < mylist.Count; i++)
{
if(mylist[i].loaded)
{
//assign mylist[i].asset to where it needs to go and mark this list element as already being processed
}
}
}
But my my game still freezes during load. So it seems like LoadAsync is actually blocking. Is there some other way I should be doing async loading of assets?
I don't know what type of list you're using for mylist
, but Resources.LoadAsync doesn't return assets, it returns a ResourceRequest instance. Try replacing that and your list with:
private List<ResourceRequest> resourceRequests;
void Start()
{
for(int i = 0; i < n; i++)
{
resourceRequests.Add(Resources.LoadAsync(path));
}
}
void Update()
{
for(int i = 0; i < resourceRequests.Count; i++)
{
if(resourceRequests[i].isDone)
{
// access with "resourceRequests[i].asset"
}
}
}
I did say I was saving the returned ResourceRequest to a list. What I have is a bit of psuedocode though. The list is actually of a class that that holds a ResourceRequest object and some other members (like loaded) since I need some more metadata in order to be able to associate the loaded asset to where it's supposed to go once loaded.
I also looked at the Unity docs on LoadAsync and it is using a coroutine. I tried that as well (starting a coroutine for each load in the for loop) and it still freezes up. I was thinking maybe I'm spawning too many coroutines in the loop, but n in the loop is only 10 which doesn't seem like that much to me.