x


Displaying a loading dialog

I'm trying to display a "loading..." dialog to the user after a button press and I must not understand something very basic. This is what I'm doing:

function OnGUI () {

if (GUI.Button (Rect (0,0,10,10), "LoadMyMesh")) {

    GUI.Label (Rect (0, 0, 100, 100), "Loading…");

    Resources.Load("nameOfMyMesh", Mesh);

}

}

..but the "Loading..." GUI.Label is never drawn on screen, despite the loading process taking significant time. What am I not understanding, or how would you do this?

Thank you.

-Greg

more ▼

asked Oct 27 '11 at 02:55 AM

ghart gravatar image

ghart
1 2 3 4

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I think the problem is that OnGUI does not have 'enough time' to redraw, since the Load operation is non preemptive.

I never actually faced this problem, but I guess your best call is to start a coroutine to load your resource, so that OnGUI keeps running in the foreground.

private bool routinelaunched = false;

private IEnumerator mycoroutine() {
  Resources.Load("nameOfMyMesh",Mesh);
  return null;
}

function OnGUI() {
  //gui stuff
  if (routinelaunched == false) {
    StartCoroutine(mycoroutine()); 
    routinelaunched = true;
  }
}

This is untested code, but it should lead you in the right direction.

more ▼

answered Oct 27 '11 at 09:24 AM

roamcel gravatar image

roamcel
1.2k 37 40 44

Thank you very much for the lead, roamcel. Below is what seams to be working for me...

private var inProgress : boolean = false; private var routineLaunched : boolean = false;

function OnGUI () {

if (GUI.Button (Rect (0,0,10,10), "LoadMyMesh")) {
    inProfress = true;
    routineLaunched = true;
}

if (routineLaunched == true) {
    StartCoroutine(mycoroutine());
    routineLaunched = false;
}

if (inProgress = true) {
    // GUI stuff
}

}

function mycoroutine () {

yield WaitForSeconds (0);   // don't wait until returning to the main routine...
Resources.Load("nameOfMyMesh",Mesh);
inProgress = false;

}

Oct 27 '11 at 10:39 PM ghart
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x309

asked: Oct 27 '11 at 02:55 AM

Seen: 500 times

Last Updated: Oct 27 '11 at 10:44 PM