- Home /
"loading image or a bar" in between scene switching
Hello folks;
i searched the answers and the forums but i could not find what i am looking for so here is my question:
i have a scene which contains multiple buttons that upon clicking take me to other scenes. everything works fine but the other scenes are a little heavy while they are loading. so i need a image or a progress bar while the new scene is loading. in the answers i see lots of people refering to "Application.LoadLevelAsync", but i could not figure out how to do that. so if you please help me about where to put "Application.LoadLevelAsync" code and displaying the image or bar while loading... Here is my code in javascript:
function OnGUI() {
if(GUI.Button(Rect(450, 160, 220, 20), "scene1")) {
Application.LoadLevel("scene1");
}
if(GUI.Button(Rect(450, 185, 220, 20), "scene2")) {
Application.LoadLevel("scene2");
}
if(GUI.Button(Rect(450, 210, 220, 20), "scene3")) {
Application.LoadLevel("scene3");
}
if(GUI.Button(Rect(450, 235, 220, 20), "scene4")) {
Application.LoadLevel("scene4");
}
}
Thank you in advance
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 TonyLi · Jan 31, 2014 at 02:31 PM
The LoadLevelAsync and LoadLevelAdditiveAsync functions require Unity Pro.
If you don't have Unity Pro, you'll have to use LoadLevel, which pauses until the level is done. It doesn't provide a way to have a progress bar. You could, however, put a static image on the screen, such as "Loading. Please wait..." while it's loading.
If you have Unity Pro, you'll probably want to run LoadLevelAsync in a coroutine so you can monitor the status of the AsyncOperation returned by LoadLevelAsync. Keep looping in the coroutine until AsyncOperation.isDone is true. Inside the coroutine loop, you can set the value of the progress bar using AsyncOperation.progress. Note that progress doesn't update very accurately when playing inside the editor, but it's much more accurate in end-user builds.
i forgot to mention that i have pro. the thing is i do not have the coding skills to add LoadLevelAsync to the code i have above. thats why i kindly ask your help :)
but i will look into the coroutine thingy... thank you again
unityAnswers isn't a free code-writing service. If you post on the Scripting forum you might get some help writing the code since this will probably require some back-and-forth discussion. Or you could offer to pay someone to do it on the Job Offering forum or buy a product on the Asset Store that does this for you, such as Scene $$anonymous$$anager or $$anonymous$$ad Level $$anonymous$$anager.
by the way, i think i got the solution. Could you be so kind to check it? var ao : AsyncOperation;
function OnGUI () {
if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 15, 200, 30), "Load Demo")){
ao = Application.LoadLevelAsync("Demo");
}
if(ao != null) {
GUI.Box (new Rect(0, 40, ao.progress * Screen.width, 40), "Loading");
}
}
Can't you just test it and see if it works? Anyway, this might work a little better:
var ao : AsyncOperation;
function OnGUI () {
var rect = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 15, 200, 30);
if (GUI.Button(rect, "Load Demo")){
ao = Application.LoadLevelAsync("Demo");
}
if (ao != null) {
if (ao.IsDone) {
ao = null;
} else {
GUI.Box (new Rect(0, 40, ao.progress * Screen.width, 40), "Loading");
}
}
}
Answer by poncho · Jan 31, 2014 at 03:09 PM
You could Use LoadLevel, But, load each of your gameobjects by yourself, and implement a loading bar with those elements If you know how many elements you are going to load, then you have your max value, then for every element to load, use a Load.Asset, after it gets loaded and added to the scene, the counter of the bar add 1, and to know the width of your progress bar is barCount/barMaxCount, do not know if thats the optimal, but it is how i use a loading screen in unity free