- Home /
Best practices for increasing scene load times in 2d games
Title says it all, im trying to optimize my loading of scenes and i can take up to 9 seconds to load a scene sometimes and other times less than 3 seconds, im trying to pinpoint what needs to be optimized but profiling this is a lot more challenging that i thought. also noticed that once a scene has been loaded once re entering it yields a much quicker load time even if the game has been restarted which i find quite puzzling.
so where should i start?
I dont think there are best practices, but you could add a loop that counts to a few millions in awake. And if you want do decrease loading times you could for example delay certain initializations until they're really needed (if you have alot of code running in awake or start). Lookup "lazy initialization" for that. Apart from that, reduce the amount of polygons/objects (lookup "Static batching"). Audio files also have an option to be preloaded or streamed.
Depending on your game/scene setup, you can load assets that you will be reusing in every scene when the game initially starts and call a DontDestroyOnLoad on them. Doing that and object pooling combined will help reduce load time.
Answer by Immanuel-Scholz · Apr 13, 2016 at 12:49 PM
I am going out on a limb here and say that you have too much stuff referenced directly by your scene that gets all pulled and loaded before the game has a chance to start.
You can verify that assumption by right-clicking your scene in the editor and choose "Select Dependencies".
If that brings up like 1000 textures and 500 audio files, then you got yourself a problem. ^^.
AFAIK, Unity preloads almost everything that is directly linked in a scene file and which is not in an asset bundle. So if that is your problem, you might try one of the following:
Clean up all unused links. (maybe you just forgot to remove some big resources from test-objects that are deactivated anyway..?)
Set big audio clips like background music to streaming.
Pack stuff that you don't need right at the start of the game in Asset Bundles and load them asynchronously
Move stuff in other scenes and load them asynchronously (and additive).
Move stuff to Resources/ folder and load them via code once the game started
Well, of course it could be that the problem is totally somewhere else. You know.. stuff like "void Awake() { Thread.Wait(1000); }". ;)
Hello there, thanks for the information, i did what you said and tested my scene for dependancies and what it selected were 24 small audio files used for the scene, i tried a few times but just kept finding the 24 audio files, im surprised it didnt bring up any sprite sheets but im guessing that the issue with slow loading isnt related to this then
As for awake functions i almost never call them, i think they are called about twice per scene, one in the player and one in the main manager, i mostly use start functions, and in some cases a few hindered are probably called each scene, though most of those are for a game mechanic which is used throughout the level
Do you have any other suggestions as to how to increase my load speed time? or is there any way to profile whats causing all the load time, anything at all would be greatly appreciated as im at a loss where to proceed from here
@ Immanuel Scholz
You have really only 24 audio files selected? No prefabs? No shader? No fonts? Not even $$anonymous$$onoBehaviours?
Well.. then have you tried profiling the scene loading? (With the unity profiler). It has its hickups with long lasting frames (e.g. some times are totally off) but it could still give you valuable insight where the time might be spent.
Sprinkle some Profiler.BeginSample/EndSample all over your suspicious code to narrow it down..