Load Model File From Web Progress

I am creating a simple 3D model viewing application. The models are loaded at run-time from a text file located on a server that the user can point to. Loading and creation of the meshes works fine, as do texturing, navigation etc. I have but one, simple problem…

A text file of approximately 1mb takes a little while to load, given the slow speed of the Edge network. It is slightly better over WiFi. I would like to show the loading and model-building progress as GUIText, so the user doesn’t sit there like an idiot not knowing what’s going on.

HERE IS THE PROBLEM: My GUIText object created for this purpose does not update when I call myGUITextObject.text = someNewText in either the ReadModelFileFromWeb or CreateModelInUnity methods.

The pseudo-code of this is as follows:

Update()
{
    if(isLoading)
    {
        ReadModelFileFromWeb();
        CreateModelInUnity();
    }
}

void ReadModelFileFromWeb()
{
    //some loading code here...

    //progress updating code...
    while(!myWWW.isDone)
    {
        myGUIText.text = myWWW.progress //myGUIText object doesn't update
    }
}

void CreateModelInUnity()
{
    //some model creation code here...

    //progress updating code
    myGUIText.text = numVertsLoaded.ToString() + " vertices loaded..."; //myGUIText object doesn't update
}

I’ve tried making the ReadModelFileFromWeb and CreateModelInUnity methods coroutines and yielding for fixed update etc…, but that doesn’t allow updating of the GUIText either. I know the GUIText works as I have other scripts that are updating it in the game, and the last update that I call in the above methods does actually change the text. It just doesn’t work during the execution of the above methods. I’m sure this has something to do with intertwined update methods, but I can’t figure it out.

Any examples of this behavior working in others’ work or suggestions are greatly appreciated.

You shouldn’t use Update for this…since Update is run every frame, it would be starting the model reading and creation code every frame. More like:

function ReadModelFileFromWeb () {
    // start loading here...

    while(!myWWW.isDone)
    {
        myGUIText.text = myWWW.progress
        yield // <-- need this
    }
}

I highly doubt the CreateModelInUnity function would need a progress indicator, as it should run in well under a second. But if it did, you’d want to either make it use a separate thread and then monitor the progress from another function, or else have it be a coroutine and yield every so often while constructing the model.

–Eric

Eric,

Thanks for that. Like I said, I originally had the reading and the model building as Coroutines (not in the update method), but I couldn’t get the yield statements right (I think there’s a different syntax for doing it in C#). So I put it inside update, so that update would just check the boolean “isLoading”. At the end of the loading loop, it sets it back to false so I didn’t think there was much cost to doing it inside Update.

You’re also right about the building method being very fast. I just put in some GUIText updating stuff because I wasn’t sure where the hanging was.

I’ll try your yielding method tonight and see how she flys. Thanks again.