[SOLVED]How to load dynamically a list of resource files & instantiate them without freezes on iPh.?

Hello there,

I just started developping on unity a few weeks ago, and learned on my own, so i may have misunderstood some mechanisms or behaviours. If so, my apology for the question i’m going to ask.

I’m actually developping a mobile game that need to frequently generate random levels as the player progress through it, so as it is now, i’m loading these levels stored as prefabs in the Resource directory of my project. I also possess a script attached to an empty object on the scene who will be in charge of loading and handling the levels generated. It works by making dynamically(as the object is instantiated) a 2Darray of the hierarchy of files where i stored my level prefabs, which will later on let me load my levels using Instantiate(Resources.Load()).

As i am aware that resource folder contents are stored into a resources.asset file on iPhone stored who knows where, i used Resources.LoadAll() for each of my level directories for listing them into my array to avoid too much path issues.

Everything works well on computer, but as soon as i pass on mobile, the game takes an insane amount of time to load the scene, also even though i used coroutines(which is supposed to simulate some threading behaviour if i did understand it right) for loading levels while the player is still actively playing, there is still some freezes each time the game has to load another level.
So, i read on multiple topics that i should be using Resources.Load/LoadAll everytime i need to access some assets, but right now it’s making me have a LOT of loading time.

I’ve checked on the profiler too, the only time where my cpu is spiking is when i need to load these levels themselves.

I would like to know if i made some conception errors, or if i misunderstood something while learning to code on Unity, and also if somebody knows some tips or ways to reduce to the utmost the resource loading time?

If a similar thread was posted before, please link it to me too, as i may have overlooked some links in my researchs too, though i’ve been looking for an answer for a few days now…

Thank you in advance for your time.

There is diffirent ways of loading objects so they don’t block the game.

Why don’t you load one object each frame or loading x objects each frame so that you can give the game space to do it’s rendering ? that way it will seem more fluent ?

You can also consider loading it in a Background Thread or using Resources.LoadAsync ?

You don’t need to use LoadAll , that doesn’t make sense?

Out of curiosity how much is “an insane amount of time” - Android phones are slower than the rest of the world so it will always be slower.

The thing is, i made a prefab containing all of my level objects, and am loading it through an Instantiate(Resources.Load(the_level_prefab)) call, so this makes it hard to split the instantiating that way, though i may want to reconsider this designing :/.

The two main moments where i operate on levels are at the beginning of the app, when i want to cache everything for optimization purpose, and when i am instantiating them while playing, and in this case i use a coroutine for loading the level in background…though it does not work as expected. I didn’t know yet of the LoadAsync function, might try that ^^

Well, for this one the problem was at the caching phase that i mentionned earlier, i want to do an 2Darray for “indexing my levels”, for easier access purpose later on. To do that, i need to get dynamically the resource path of each of my directory containing levels. I tried many things here, none worked, so i instead made a loadAll of some hard coded resource directory path, so that i could get a list of the level prefab that i wanted to access from the beginning. I know, this sucks, and i would like tips on that too if possible :stuck_out_tongue:

Well, i’m actually taking ~20 seconds to load my levels(~250 prefabs of between 100-200 objects to load) at the start of the app on iPhone 6, and ~0.4 freezing time for instantiating the levels in game…I’ll let your imagination go wild as for how much time this should take on Android ^^"

Well, just replying to say that everything has been smoothed out thx to playing with this LoadAsync() method, and some coroutines. I didn’t even need to modify my designing too much around lol. Thanks a lot.