How to preload scene and prefabs so it won't hiccup when first time used?

Hi all,

First of all, I know this have been asked before, and i’ve spend a day looking for the best answer, which i can’t find or the threads is really old. So i try to make a new post here. I’m new with unity.

So basically i had a scene, where all of the objects will be Instantiate from a script(the world, characters, visual effects as particle explosion, gunshot, etc). I’m using the Resources.Load(“assets path”) method.

My current problem is when a particle effect(explosion or cracked flame) is instantiated, it’ll hiccup for a sec or two.
And I’m afraid it’ll also happen when i play sounds.

My conclusion is I need a preloader scene or a methods to load all the assets that i need to remove the hiccup.

Some say that i should create empty preload scene and then load my gamescene using LoadLevelAsync, but it requires unity pro, i only have the basic right now.
My thought is loadLevelAsync will only load the prefabs that already in the scene, not that generated on runtime using Resources.Load. is it correct?

the other solutions i found is to instantiate everything on the first frame and then destroy it. which i found is not a good approach to do.:(.

Please someone, shed me enlightenment for this problem? A method to make a preloader for a scene and it’s assets.
I develop for iOS, and for illustration, what i’m trying to make is kinda like in fighting game… where the player choose the character and the stages. and then it change to Loading scene where it load the world and character(with all the assets, effect, sound, etc). and then after loading done, it’ll go to the gamescene, so the gameplay is seamless.

I’ll get unity pro if it can solve this problem.

Thanks a lot,
daniel.

You are probably trying to load all assets in 1 frame…you can write your own async function that instantiates all objects over time

i’m not sure if i understand your problem correctly. but when you have a loading screen why dont you load all assets there?

if you load the assets in the loading scene and then switch to another scene the assets are gone unless you declare them dontdestroyonload (makes it messy when you load the scene several times as they are created again each time).
Unity - Scripting API: Application.LoadLevelAdditive the docu does not state it requires unity pro so you could give it a try.
other possibility is to have the scene in question load all assets at start and during its loading display a fullscreen gui stating its loading. with unity indy this cant be animated (loading bar) afaik.
also between each resources.load call you should yield the loading function to give control back to unity and handle input etc that user dont gets impression your application is hung.
not sure about the following:
if ongui is called when you yield you could also animate a loading bar in unity indie. when you have 20 assets for example to load you add 1 to a variable each time you loaded one, call yield and display a bar / texture with the percentage (1 / 20 etc). but you have to try out or ask a more experienced developer.

hope that helps.

thanks for fast reply guys,

@Bluee.Eyess → sorry, but I don’t quite understand about the async function methods to instantiate game object over time, can you be more spesific?

@exiguous → your second suggestions about yield while loading is interesting, however, i can’t find a sample code that show how to do that, any example please? thanks alot…

Sorry guys, I just a noob trying to learn something here, please be patient with me.
anyway, i just tried to load the visual effects prefabs (Resources.Load(Path)) of a gameObject in it’s Awake() function, but it’s still hiccup for half a second when it instantiated for the first time. the visual effects mostly are particle effects or a plane with animation in it.
almost forgot to say, the hiccup only occured when i build for iOS, if i build for PC/Mac it works fine. But it still a problem since my target is iOS/ mobile.

thanks.

i have not read about that before (neither tried) i just activeted my phantasy. so if you would like to know if its a solution for your problem you probably would have to try it out (unless an expert can verify or falsify this).

the best you learn from your own research and trying around. if you get a ready solution for any of your wishes you have learned nothing. and my patience is not big enough to waste my time for preventing a “newbie” from investing time and effort in his aims. and i’m not sorry for this.

I’m agree with you exiguous, the best way to learn are through trial and error. And i’ve spent quite some times, playing with the code and searching in the net before posting the problem here, to make sure that I’m really stuck.

I tried the Instantiate and destroy methods when the scene started, and it only works if the objects instantiate within the camera range and the scene takes sometimes to load. I’m still thinking this method is not a good way to code.

@Bluee.Eyess → did you mean the load async function is to instantiate the object in different frame?
i think the problem that causing the hiccup is not the instantiate of the object, but because the iphone need sometimes to read the texture of the prefabs.

to load an enormous amount of prefabs, i just put them in the resources folder and then load them to an array or a dictionary and then i store them in a static class where i store global game information, then when i need them i just instantiate them and they do it quickly because they are already loaded in memory, not too recommended for huge amounts of prefabs in mobile because of the low memory

@CoatlGames could you give a peek into that code :wink:
putting them in the resource folder means you there in your project tab on build? or …
where when and how do you load them into an array?

sorry for the noob question, trying to learn more about loading/performance and memory use…

thx
J.

1 Like