- Home /
The question is answered, right answer was accepted
How to make a loading feedback for big GameObjects loading?
I already have one answer to a part of this question: by using the yield statement, for example:
private var loading : boolean = false;
function loadMyMesh()
{
loading = true;
var myNewGO : GameObject = Instantiate(Resources.Load("models/bigbigmesh", GameObject));
yield;
loading = false;
return myNewGO;
}
function OnGUI()
{
if (loading)
{
GUI.Box(Rect(0,0,50,10), "LOADING...");
}
}
This is the first time I use "yield", and I have not understood everything about coroutines yet. I'm not sure this is the best workaround for my problem, and whereas this code works fine in the editor, this doesn't work while deployed for the web player. If I try to load something on the web player, the object is loaded, then the message "loading..." appears for one frame, then disappears.
What am I doing wrong?
Add a timer or something to that effect that makes loading = false; after the period of time you choose.
$$anonymous$$aybe WaitForSeconds or something like that.
Seriously though, it won't take very long to Instantiate an Object.
Thank you for the answer In my case, it takes 1 to 3 seconds to load some of my models and I don't want the user to think that the application froze. I will try WaitForSeconds as you said, thank you.
The issue is an extreme pain in the ass
http://answers.unity3d.com/questions/433609/hiccups-when-loading-images-from-resources-folder.html
http://answers.unity3d.com/questions/393460/resourcesload-async-for-large-images.html
the fact that you can't stream large stuff in to Unity (other than audio, which is handled perfectly) is a total pain in the ass.
Answer by Croustibat · Oct 10, 2013 at 02:26 PM
Ok I got it working. This is the steps I followed:
you made me understand the following thing: at the line an Instantiate or a Resources.Load is called, no Update or OnGUI is called until it's done. That's why my line "loading = true;" was not taken into account in the OnGUI monobehaviour until the loading was done.
so, I changed my code to the following:
function loadMyMesh() { loading = true; yield; var myNewGO : GameObject = Instantiate(Resources.Load("models/bigbigmesh", GameObject)); loading = false; return myNewGO; }
It's still working in the editor, but a strange problem came: the function "loadMyMesh" doesn't have the same behaviour when it is called by the navigator through javascript "SendMessage" (see [here][1]) as when the same function is bound to an event such as Input.GetKeyDown() In the first case, the yield; doesn't make any difference, whereas it's working fine when the function is bound to a keyboard event. To solve the problem with the least modifications, I first tried the meat500's solution, which worked out perfectly, thus I still don't really understand what's been going on. Anyway, here is my final code:
function loadMyMesh()
{
loading = true;
yield WaitForSeconds(.1);
var myNewGO : GameObject = Instantiate(Resources.Load("models/bigbigmesh", GameObject));
loading = false;
return myNewGO;
}
Thank you for your help! [1]: http://docs.unity3d.com/Documentation/Manual/UnityWebPlayerandbrowsercommunication.html
Answer by Minizinger · Oct 10, 2013 at 11:54 AM
I don't know how to use yield in this way. In my code I'm just using something like this:
using UnityEngine;
using System.Collections;
public class sas : MonoBehaviour {
bool loading = true;
GameObject SomeObject;
void Update () {
if(SomeObject == null){
SomeObject = GameObject.Find("Name of this object");
loading = true;
}
else
loading = false;
}
}
hope it will help.
Follow this Question
Related Questions
Resources.Load Instantiate Yield problems 1 Answer
Respawn after delay 3 Answers
Instantiating many objects during update lags 1 Answer
Respawn after delay 3 Answers