0

这是我对服务器的发布请求:

public WWW POST(string url, string post) 
{ 
    var www = new WWW(url, Encoding.UTF8.GetBytes(post));

    StartCoroutine(WaitForRequest(www));
    while (!www.isDone && www.error == null)
    {
        Console.Write("downloading...");
        Thread.Sleep(1000);
    }
    return www;
}

private IEnumerator WaitForRequest(WWW www)
{
    while (!www.isDone && www.error == null) { yield return new WaitForSeconds(0.1f);}

    // check for errors
    if (www.error != null)
    {
        Debug.Log("WWW Error: " + www.error);
    }
}

它在 Unity 编辑器中运行良好,但在 Web Player Build 版本中冻结为无限循环。有人知道为什么吗?

4

1 回答 1

0

好的,问题出在协程中

    public IEnumerator LoginFinished(string message)
    {
        string url = "URL";
        Console.Write("Post on: " + url);
        var response = _httpServer.POST(url, message.Substring(message.IndexOf('?') + 1));
        while ( !response.isDone && response.error == null )
        {
            yield return new WaitForEndOfFrame();
        }
        Console.Write("response: " + Zlib.Unzip(response.bytes));
    }


    public WWW POST(string url, string post)
    {
        var www = new WWW(url, Encoding.UTF8.GetBytes(post));
        StartCoroutine(WaitForRequest(www));
        return www;
    }

    private IEnumerator WaitForRequest(WWW www)
    {
        yield return www;

        if (www.error != null)
        {
            Debug.Log("WWW Error: " + www.error);
        }
    }

如果我做

StartCoroutine(LoginFinished("authdata"));

它工作得很好。

于 2014-05-05T11:54:15.283 回答