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

查看:36
本文介绍了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'的值.但是'返回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天全站免登陆