在Unity中加载文件时等待 [英] Wait while file load in Unity

查看:133
本文介绍了在Unity中加载文件时等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从其他方法加载和返回文件(使用WWW)? 我想下一步:

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

  1. 方法GetSettings().下载文件文本,解析json并 返回结果.
  2. Start()调用方法,并等待GetSettings()返回结果.
  1. Method GetSettings(). Download file text, parse json and return result.
  2. Call method from Start() and wait while GetSettings() return result.

我该怎么做?

推荐答案

看起来像您要下载数据,然后等待,然后等待下载完成,然后再下载其他数据.如果是这样,您可以通过下面的代码下载两次数据.您可以通过增加REQ_AMOUNT值来增加次数.

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.

它使用yield return StartCoroutine等待当前协程函数返回,然后再次运行.

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
}

这篇关于在Unity中加载文件时等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆