WWW.Post在编辑器中工作正常,但在WebPlayer Build中没有

时间:2014-05-02 08:40:17

标签: post web unity3d yield unity-web-player

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

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 Editor中运行良好,但它在Web Player Build版本中冻结为无限循环。有人知道为什么吗?

1 个答案:

答案 0 :(得分: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"));

它运作得很好。