Vostrugin
Vostrugin

Reputation: 160

Wait while file load in Unity

How I can load and return file from another method(use WWW)? I want to do next:

  1. Method GetSettings(). Download file text, parse json and return result.
  2. Call method from Start() and wait while GetSettings() return result.

How I can do this?

Upvotes: 0

Views: 2380

Answers (1)

Programmer
Programmer

Reputation: 125435

Looks like you want to downloaded data then wait then wait for the download to finish then download another data. If this is true the you can the code below will download data 2 times. You can increase the number of times by increasing the REQ_AMOUNT value.

It uses yield return StartCoroutine to wait for the current coroutine function to return before running again.

IEnumerator Start()
{

    int REQ_AMOUNT = 2;

    for (int i = 0; i < REQ_AMOUNT; i++)
    {
        yield return StartCoroutine(GetSettings());
    }
}

IEnumerator GetSettings()
{
    string url = RoomSettings.AbsoluteFilenamePath;

    if (Application.isEditor)
    {
        url = "file:///" + url;
    }

    var www = new WWW(url);
    yield return www;
    // Do some code, when file loaded
}

Upvotes: 1

Related Questions