Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...