Unity-等待HTTP请求解决 [英] Unity - Waiting for HTTP request to resolve

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

问题描述

这里是函数,我想知道是否有更简洁的编写方法:

Here is the function, I am wondering if there is a more concise way of writing it:

private static WWW WaitUntilResolved (WWW request)
{
    bool success = true;
    float timeout = 5000, timer = 0;

    while (!request.isDone) {
        if (timer > timeout) {
            success = false;
            break;
        }
        timer += Time.deltaTime;
    }

    if (success && request.error == null)
        return request;
    else {
        request.Dispose ();
        return null;
    }
}

ps. WWW是本机统一类: https://docs.unity3d.com/ScriptReference/WWW.html

ps. WWW is a native unity class: https://docs.unity3d.com/ScriptReference/WWW.html

推荐答案

您确实在使用WWW isDone 错误.如果必须使用isDone,则必须将其放入while循环中.您还必须使用yield return null;在while循环中屈服,否则游戏将冻结直到下载完成.这整个过程需要协程,因此该函数必须成为协程函数.

You are really using WWW and isDone wrong. If you must use isDone, you must put it in while loop. You must also yield in the while loop with yield return null; otherwise the game will freeze until the download is done. This whole thing requires coroutine so the function must be made a coroutine function.

您真的不需要isDone.当您想了解下载进度时,仅使用 .

You really don't need isDone. That's only used when you want to know the progress of the download.

这是使用isDone的正确方法:

private static IEnumerator WaitUntilResolved(WWW request)
{
    while (!request.isDone)
    {
        Debug.Log("Download Stat: " + request.progress);

        //Wait each frame in each loop OR Unity would freeze
        yield return null;
    }

    if (string.IsNullOrEmpty(request.error))
    {
        //Success
    }
}

如果您不需要了解下载进度,则应使用以下内容:

If you don't need to know the progress of the download then below is something you should use:

private static IEnumerator WaitUntilResolved(WWW request)
{
    yield return request;
    if (string.IsNullOrEmpty(request.error))
    {
        //Success
    }
}

最后,您不能直接调用协程函数.您必须使用 StartCoroutine 函数来做到这一点:

Finally, you cannot call a coroutine function directly. You have to use StartCoroutine function to do that:

WWW www = new WWW("www.yahoo.com");
StartCoroutine(WaitUntilResolved(www));


如何为协程设置超时?

How do I set a timeout for the coroutine?

大多数WebRequest教程都使用计时器.您无需在Unity中使用WWW API进行此操作.在此没有必要,因为这是非阻塞操作.如果必须,请参见下面的代码.

Most WebRequest tutorials uses timer. You don't need to do that in Unity with the WWW API. It is not necessary here because this is a non blocking operation. If you must then see the code below.

private static IEnumerator WaitUntilResolved(WWW request)
{
    float timeout = 5000, timer = 0;

    while (!request.isDone)
    {
        Debug.Log("Download Stat: " + request.progress);

        timer += Time.deltaTime;
        if (timer >= timeout)
        {
            Debug.Log("Timeout happened");
            //Break out of the loop
            yield break;
        }
        //Wait each frame in each loop OR Unity would freeze
        yield return null;
    }

    if (string.IsNullOrEmpty(request.error))
    {
        //Success
    }
}

如果您需要返回下载状态,请参阅帖子,其中介绍了如何执行此操作.

If you need to return the status of the download, see this post that explains how to do that.

这篇关于Unity-等待HTTP请求解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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