Unity-仅在协程完成后才需要返回值 [英] Unity - need to return value only after coroutine finishes

查看:689
本文介绍了Unity-仅在协程完成后才需要返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在统一开发游戏,遇到了我无法解决的问题. 我正在通过标准WWW对象并使用协程连接到Web服务器,以便执行POST请求.

I'm working on a game in unity and encountered an issue which I cannot solve. I'm connecting to a web server via standard WWW object and using a coroutine in order to execute a POST request.

代码本身可以工作,但是我需要更新一个变量值,并在协程完成后返回该变量,这是我做不到的.

The code in itself works, but I need to update a variable value and return that variable once the coroutine finishes, which I'm not able to do.

public int POST(string username, string passw)
{
    WWWForm form = new WWWForm();
    form.AddField("usr", username);
    form.AddField("pass", passw);

    WWW www = new WWW(url, form);

    StartCoroutine(WaitForRequest(www));

    //problem is here !
    return success_fail;
}

private IEnumerator WaitForRequest(WWW www)
{

    yield return www;
    if (www.error == null)
    {

            if(www.text.Contains("user exists"))
            {

                success_fail = 2;
            }
            else
            {
                success_fail=1;
            }
    } else {
        success_fail=0;
    }    
}

协程使用相关值更新'success_fail'的值. 但是返回成功失败";协程完成之前,POST方法中的代码行会运行,这会导致它返回错误的值.

The coroutine updates the value of 'success_fail' with the relevant value. But the 'return success_fail;' line in the POST method runs before the coroutine finishes, which causes it to return a false value.

我尝试使用其他协程,但未成功,假设我也有错误. 仅在协程完成后,如何才能返回"success_fail"值?

I've tried to use an additional coroutine but unsuccessfully, suppose that I had a error there as well. How I can return the 'success_fail' value only after the coroutine finishes?

谢谢.

推荐答案

只有协程可以等待另一个协程.由于您需要等待启动的协程(WaitForRequest),这意味着您必须将POST转换为协程,并且它无法返回int.

Only a coroutine can wait for another coroutine. Since you need to wait for the coroutine you started (WaitForRequest), it means you have to convert POST to be a coroutine and it won't be able to return int.

success_fail似乎是一个成员变量,因此,如果暴露给启动POST的人(作为协程),则无论如何都无需返回它.

It looks like success_fail is a member variable, so if that's exposed to whoever is starts POST (as a coroutine), you wouldn't need to return it anyway.

public int success_fail

IEnumerator POST(string username, string passw)
{
    WWWForm form = new WWWForm();
    form.AddField("usr", username);
    form.AddField("pass", passw);

    WWW www = new WWW(url, form);

    yield return StartCoroutine(WaitForRequest(www));
}

private IEnumerator WaitForRequest(WWW www)
{
    yield return www;
    if (www.error == null)
    {
        if(www.text.Contains("user exists"))
            {
                success_fail = 2;
            }
            else
            {
                success_fail=1;
            }
    } else {
        success_fail=0;
    }    
}

基本上,如果您希望代码等待",则必须是协程.您不能进行不阻塞整个引擎的等待呼叫(没有某种类型的循环攻击).

Basically, if you want your code to "wait", it has to be a coroutine. You can't make a call that waits without blocking the whole engine (without some type of loop hack).

此线程提供了一种方法,您可以在需要时从协程返回int,但POST仍然不能成为阻塞调用...

This thread gives a way where you could return the int from your coroutine if you really need to, but POST still can't be a blocking call...

http://answers.unity3d.com/questions/24640/how-do-i-return-a-value-from-a-coroutine.html

这篇关于Unity-仅在协程完成后才需要返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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