- Home /
How to make a dynamic loading scene?
So I searched for this a lot, but most of the answers were not what I was looking for and the others were just going over my head. So I have created a scene called "loading" and have animations and all the required stuff here. I want this scene to show every time I switch from one scene to another. I have 3 other scenes called "main", " menu" and "store" and I want to add that loading scene between these. I can't workout how can I do this. Also I wanna know how can I make the things load during the loading screen so that they don't have to be loaded later on. Thank you, let me know if I am not clear anywhere :)
Answer by fafase · Oct 14, 2015 at 04:24 AM
public void HandleSwitchLevel(){
StartCoroutine(Loading("Level"));
}
private bool loadingItem = false;
IEnumerator Loading(string level){
this.loadingItem = true;
while(loadingItem){
Animate();
yield return null;
}
Application.LoadLevel(level);
}
Add the method as listener to your scene buttons, I guess the player can change scene via a button press.
If you need to perform other actions then add them also as listener. If you need to wait for those action to be done while loading then you need to create a connection between Loading and the other method. Using an event is most likely.
public UnityEvent OnLoaded;
public void MethodLoadingItemsForStore(){
StartCoroutine(LoadingForStore());
}
IEnumerator LoadingForStore(){
while(loadingStuff){yield return null;}
OnLoaded.Invoke();
}
And your other class with Loading is registered to OnLoaded with that method:
public void HandleOnLoaded(){
loadingItem = false;
}
Great, I will try it asap. BTW, in your first code part, when a button is pressed, can it not switch to a scene called "loading" and switch to "level" string after things have loaded. Also, how do I make the things load. I have added everything I want to load to Resources folder, do I simply use Resources.Load()? Thanks
Okay so in the first code, I tried it. But the thing is, it keeps loading forever and never switches from while loop to Application.LoadLevel(level) below that. I tried doing Debug.log("loading"); in while loop and realised it kept going on forever and never switched to the scene I wanted it to (restart).
About the second code, I can't seem to get the OnLoaded work, basically I don't find "UnityEvent". Do I need to include a namespace?
Thanks
Yes, you need a namespace. Right click on UnityEvent then resolve and using (don't remember the namespace, probably some UnityEngine.EventSystem).
Then the event will show in the inspector and you can drag an object and select the method, just as you did for the button.
The loading will not stop since there is no condition to make it stop. This is why you need the second class.
You could have something in Animate() that defines when to switch but it would be arbitrary while you want all other things to be done and then switch.
$$anonymous$$akes sense?
I am still unable to get it to work :/ Okay here's the detail :
I have a scene called "main" where all the things happen. This scene has a restart button and a goto menu button which do their respective functions. However, when switching between scenes the game freezes for a while, ofcourse for loading stuff. Therefore I want a loading screen between them.
So, in my "main" scene, I have a gameObject called "UI$$anonymous$$anager" which is attached to a script called "UI$$anonymous$$anager" and a "LoadControl" gameObject attached to a script called "LoadingScript".
EDIT EDIT EDIT : Please ignore some of the above written things. I forgot to call HandleRestartLevel() in my script. But now that I have done it, the level still won't stop loading and keeps loading forever. Basically, the "loading" scene keeps going forever. in "UI$$anonymous$$anager" //restarting level public void restartLevel() { loadScript.HandleRestartLevel (); }
in "LoadingScript" public void HandleRestartLevel() { StartCoroutine(Loading("main")); }
private bool loadingItem = false;
IEnumerator Loading(string level)
{
this.loadingItem = true;
while (loadingItem) {
Application.LoadLevel("loading");
yield return null;
}
OnLoaded.Invoke ();
Application.LoadLevel (level);
loadingItem = false;
}
Once you got it all done elsewhere, that is all of the loading of items, then you need to call a method on that object to stop the loading by setting isLoading to false.
Note that if you are not doing the other actions in coroutine then this is pointless as the other actions will keep things hanging.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to break a sprite into shapes with script 1 Answer
Why is my character strafing around the mouse (2d) 1 Answer
Simple Jump Script Help 1 Answer