- Home /
How to preload a scene with progress bar so it can start instantly?
So we have a huge level that takes like 10 seconds to start up from the moment we switch to it's scene - so I need to have the level preloaded with a progress bar (or at least some kind of animation) to cover it.
I found Application.LoadLevelAsync which turns out to be useless. It does let me load the level asynchronously for ten or so frames, but the problem is that it still takes the same amount of time after the LoadLevelAsync is done. It appears that the problem is that all the assets used by the level aren't actually loaded until we actually switch to the scene.
So how can I load the assets used by a scene asynchronously while still getting to draw the current scene? If I can't do that directly in one operation, how can I find what the assets used by a scene are, and how can I then preload those assets?
thanks!
P.S. since people asked for code, below is some loading code I use that isn't solving the problem - basically the async operation finishes very quickly, but then the loader screen stops drawing for many seconds until the loaded level draws it's first frame
public class MainMenu: MonoBehaviour { public int counter = 0; public AsyncOperation loadOp;
void Start()
{
loadOp = Application.LoadLevelAsync("freewayLevel");
}
void OnGUI()
{
string loadString = "Loading "+loadOp.progress.ToString()+"%";
//TEMP: trying to see if loadlevelaync works or not...
counter++;
loadString += " (" + counter.ToString() + ")";
GUILayout.Label(loadString);
}
}
Hi Joshua, we have pro, the problem is the pro function Application.LoadLevelAsync is completely useless as it's async load finishes in a small time, and it still takes like 10 seconds after that for anything to get drawn :P
Hi, i have also tried to create a loader. everything work fine on mac/pc but not on console like VITA or Ouya as my scenes are too heavy (and already optimized...). Any change to share something aout loader and co-routine to load GO in the next scene please ?
Incrementally load your scenes in Unity3D (free) with loading bar! Using Unity 4.6 new GUI system or OnGUI! (will also work with NGUI)
Answer by thienhaflash · Jan 14, 2012 at 04:00 AM
The real problem here is after loading complete, Unity3D need some time to init all scenes assets (activate all game objects / scripts takes time) so try to disable all GameObject on your next scene (in the editor) then when loading complete, you will need to manually (by code) activate one by one GameObjects and update the progress.
Hope it useful :)
That was what I did with my project, I made a co-routine that looped through and enabled each object in the scene one by one. I think you'll find that it's not actually the LOAD time that is slowing you down, it is the time to enable all the objects.
Do what thienhaflash says, and make a routine that loops through everything in the scene.
In my project, I covered this up with a black screen and some GUI text that displayed a percentage loading. This loop has a 'yield' in it.
Hi Thienhaflash and Tasarran, I'm in the same situation as Thorbain is, so I tried your solution. However I can't say it works for me. I have a scene which loads 7sec. Then I have this same scene duplicated and I disabled ALL objects in it. And guess what. Original scene loads 7sec and this new "pretending-to-be-empty-but-actually-it-is-not" scene loads the same 7sec:( And I do no enabling so far. If I try to load really empty scene from the same point it takes max 0.5sec.
Do you have any ideas why? I guess your solution is working because you do a HUGE amount of computation in Awake or Start on some GO but your scene does not contains so many meshes, textures or whatnot which my did. And therefor your loading is because of your computational scripts but $$anonymous$$e because of loading textures and music into the memory?
All loading times were measured within editor.
How are you getting all of your disabled GameObjects? The only thing I've found that can round up disabled gameobjects is FindObjectsOfTypeAll but it also tends to return a lot of garbage which I haven't figured out how to filter without an editor class.
Answer by Bunny83 · Apr 29, 2011 at 09:26 PM
If you have Unity pro you can use Application.LoadLevelAsync like you've mentioned. It does exactly what you want. When you call this function it returns immediately and you can check isDone or progress if the loading is done. You can even yield in a coroutine and wait for it to complete. During this time you can do what ever you like.
If you don't have pro the answer is simple: You can't do anything about it, you have to wait till it's loaded (or buy pro ;)).
We do have pro, and like I said we are using LoadLevelAsync, but it doesn't solve the problem. It only takes like 100 milliseconds for the AsyncOperation to finish, at which point the new scene gets loaded and then it hangs for 10 seconds as it tries to load all the assets needed for the scene, because LoadLevelAsync doesn't actually load the scene's assets, it just loads the scenes (btw, the AsyncOperation never actually returns success for isDone, because it loads the new scene the moment it is done, and progress is always 0 as well)
I never used LoadLevelAsync yet but i don't believe that it's not working. According to the docs all assets are loaded in the background. $$anonymous$$y guess is there have to be something else wrong. For what platform are you developing? Web or Standalone? Streamed web games are a totally different story. In a streamed web player you have to check if a certain level can be loaded. Application.GetStreamProgressForLevel will do the job. On standalone everything should be find.
It's standalone, not web.
LoadLevelAsync seems to work except there is still a multi-second hang after the load completes until the new level draws. IT's still got that delay with all the scripts deleted. The only thing that makes that delay any faster is to delete all the geometry - the delay after the async operation seems to be a function of the amount of level content, which is what leads me to think that the assets for the level are not actually loaded by LoadLevelAsync.
So Bunny, what exactly do you do for scene transitions? What's your experience with scene transitions?
Well, unfortunately i was never in need of loading content in game ;). I've only developed a hack&slash multiplayer game where your play in only one level. According to the docs LoadLevelAsync should be able to load a level in background without noticeable lag. $$anonymous$$aybe your scene is just too big (well it's just a guess). We also had such lags with 64 bit OS versions. Can't remember if it was vista or win7, anyway we didn't had this issue on Win XP. Currently i don't have a solution for that. You could file a bugreport, maybe you get some feedback from the source ;)
Just got to trying to deal with this in my current game and I get exactly this problem - LoadLevelAsync or LoadLevelAdditiveAsync complete but then there is a massive pause afterwards. Very noticable on an iPhone. Wondering if anyone has found a solution - enabling the objects in order might work - but I have a feeling that order might end up being important and that could lead to some very nasty bugs.
Answer by DaveA · Apr 29, 2011 at 09:47 PM
What if you use Appliciation.LoadLevelAdditive http://unity3d.com/support/documentation/ScriptReference/Application.LoadLevelAdditive.html
Have the 'loading' part in an intermediate scene, so it loads fast, then use LoadLevelAdditive to load the 'next scene' into the one with the loading bar.
Thanks for the suggestion Dave - however I just tried LoadLevelAdditive and LoadLevelAdditiveAsync and the both have the same problem - it only takes a small amount of time for LoadLevelAdditive to complete, but then it takes like 10 seconds for the new scene to actually get anything drawn and the system hangs during that time. The problem seems to be that switching to a scene does not load any of the assets used by a scene.
Hmm. Can you post any code? Those funcs are supposed to work just like this, not freeze up (async anyway) and your initial scene (the loading bar) should show up quick, then assu$$anonymous$$g you LoadAdditiveAsync right away, stuff should come in. It might all pop in at once, but at least the load bar should work. If you need it to 'trickle in' you could break it into even more scenes to load add-async.
Hey Dave, I added code for the loader. Note there are lots of other Unity answers and forum posts about LoadLevelAsync sucking in various ways (crashes for some, progress value always 0, .9 or 1, never anything else, and my specific problem of the level still taking a long time to load even after the async op). I've kind of given up on the LoadLevel variants as it's clear they are useless and Unity doesn't care or respond to the bug reports on it- now I just want a workaround or alternative strategy so I can make a good product.
For the project I'm working on we are using a SQLite database to read in objects over time, and instantiate them using the Resources.Load() functionality. We have entire sections of the game world loading and unloading asynchronously, with the player still even being able to play. Depending on the amount of objects being loaded or how fast you want them to load, there is very little lag. It'd be very easy to integrate a progress bar into that (In fact we plan to).
I don't know if this solution would work for you, as it would probably require a major restructing of your current project :/
$$anonymous$$, sorry to hear that, I've not used these API's much, so I haven't run into those problems. $$anonymous$$ZGS: Sounds great! I've been working on something similar, also dynamically loading external files (like .obj) on the fly.
Answer by thienhaflash · Jan 16, 2012 at 03:28 AM
You can have an empty scene as the default scene so the initial waiting time (showing the default Unity logo) comes to minimum, after that using loadLevelAsyn to load the big scene. As I said before, for your big scenes, you must manually disable all GameObjects then using a looping code to active them one by one while showing a progress bar. Try it once man, you can measure the actual progress without any lag at all !
Tell me if that's still sound strange or difficult to you I will send you a sample project doing that. It's really working my side, greatly works on both editor or an iPhone
Answer by raphael.du · Mar 09, 2012 at 12:00 PM
I had the same problem with progress being only 0 or 1. This was solved by 3.5.0 release on 02/14/12.
Your answer
Follow this Question
Related Questions
How to write text or bars in a 3d scene?? 3 Answers
Heath Bar and Enemy 1 Answer
Using loading bar in editor 0 Answers
Loading Bar Until Ad Show Up 0 Answers
Load level with lives based on score 1 Answer