更新到统一后 2021.1.13f1 isNetworkError and is Http Error is already obsolete

if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }

有谁知道如何将此代码正确替换为更新的代码

stack overflow After updating to unity 2021.1.13.f1 isNetworkError and isHttpError is already obselete
原文答案

答案:

作者头像

从 Unity 2020.2 开始,您现在使用 UnityWebRequest.result

来自 UnityWebRequest.Get 的例子

using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
{
    // Request and wait for the desired page.
    yield return webRequest.SendWebRequest();

    string[] pages = uri.Split('/');
    int page = pages.Length - 1;

    switch (webRequest.result)
    {
        case UnityWebRequest.Result.ConnectionError:
        case UnityWebRequest.Result.DataProcessingError:
            Debug.LogError(pages[page] + ": Error: " + webRequest.error);
            break;
        case UnityWebRequest.Result.ProtocolError:
            Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
            break;
        case UnityWebRequest.Result.Success:
            Debug.Log(pages[page] + ":nReceived: " + webRequest.downloadHandler.text);
            break;
    }
}

或者来自 UnityWebRequest.Post 的更简单的例子

WWWForm form = new WWWForm();
form.AddField("myField", "myData");

using (UnityWebRequest www = UnityWebRequest.Post("https://www.my-server.com/myform", form))
{
    yield return www.SendWebRequest();

    if (www.result != UnityWebRequest.Result.Success)
    {
        Debug.Log(www.error);
    }
    else
    {
        Debug.Log("Form upload complete!");
    }
}
作者头像

我就是这样解决我的!

而是使用:if(www.isNetworkError)

我写道:如果(www.result == UnityWebRequest.Result.ConnectionError)

完整示例如下:

 IEnumerator Get(string url)
{
    using (UnityWebRequest www = UnityWebRequest.Get(url))
    {
        yield return www.SendWebRequest();

        if (www.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.LogError(www.error);
        }
    }
}
作者头像

将“www.isHttpError”替换为“(www.result==UnityWebRequest.Result.ProtocolError)”,将“www.isNetworkError”替换为“(www.result ==UnityWebRequest.Result.ConnectionError)”。

假设 www 表示请求对象。