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

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

问题描述

这里是函数,不知道有没有更简洁的写法:

private static WWW WaitUntilResolved (WWW 请求){布尔成功 = 真;浮动超时 = 5000,计时器 = 0;而 (!request.isDone) {如果(计时器>超时){成功=假;休息;}定时器 += Time.deltaTime;}如果(成功&& request.error == null)退货请求;别的 {request.Dispose();返回空;}}

ps.WWW 是一个原生的统一类:https://docs.unity3d.com/ScriptReference/WWW.html

解决方案

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

你真的不需要isDone.这在您想知道下载进度时使用.

这是使用 isDone 的正确方法:

私有静态IEnumerator WaitUntilResolved(WWW请求){而 (!request.isDone){Debug.Log("下载状态:" + request.progress);//等待每个循环中的每一帧否则Unity会冻结收益率返回空;}if (string.IsNullOrEmpty(request.error)){//成功}}

如果您不需要知道下载进度,那么您应该使用以下内容:

私有静态IEnumerator WaitUntilResolved(WWW请求){产量退货请求;if (string.IsNullOrEmpty(request.error)){//成功}}

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

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

<小时>

<块引用>

如何为协程设置超时时间?

大多数 WebRequest 教程使用计时器.您无需在 Unity 中使用 WWW API 执行此操作.这里没有必要,因为这是一个非阻塞操作.如果您一定要查看下面的代码.

私有静态IEnumerator WaitUntilResolved(WWW请求){浮动超时 = 5000,计时器 = 0;而 (!request.isDone){Debug.Log("下载状态:" + request.progress);定时器 += Time.deltaTime;如果(计时器>=超时){Debug.Log("超时发生");//跳出循环产量中断;}//等待每个循环中的每一帧否则Unity会冻结收益率返回空;}if (string.IsNullOrEmpty(request.error)){//成功}}

如果您需要返回下载状态,请参阅这篇帖子,其中说明了如何执行此操作.>

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 is a native unity class: https://docs.unity3d.com/ScriptReference/WWW.html

解决方案

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.

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

Here is a proper way to use 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
    }
}

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));


EDIT:

How do I set a timeout for the coroutine?

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天全站免登陆