从服务器下载纹理时出错m_InstanceID!= 0

时间:2016-09-22 01:39:33

标签: c# unity3d server textures webrequest

尝试从服务器下载纹理时,我在Unity 5.4中收到此错误。

这是代码(链接应该有效):

     UnityWebRequest www = UnityWebRequest.GetTexture("https://0.gravatar.com/avatar/fc2beef90fad49f83d79650a10b5c030?s=256&d=identicon&r=G");
     www.SetRequestHeader("Accept", "image/*");
     async = www.Send();
     while (!async.isDone)
         yield return null;
     if (www.isError) {
         Debug.Log(www.error);
     } else {
         tex = DownloadHandlerTexture.GetContent(www);    // <-------------------
     }

错误如下所示:

m_InstanceID != 0
UnityEngine.Networking.DownloadHandlerTexture:GetContent(UnityWebRequest)

1 个答案:

答案 0 :(得分:1)

这是一个错误。当www.isDoneasync.isDoneDownloadHandlerTexture一起使用时会发生这种情况。

解决方法是在调用yield return null;之前等待yield return new WaitForEndOfFrame()DownloadHandlerTexture.GetContent(www);的另一帧。

UnityWebRequest www = UnityWebRequest.GetTexture("https://0.gravatar.com/avatar/fc2beef90fad49f83d79650a10b5c030?s=256&d=identicon&r=G");
www.SetRequestHeader("Accept", "image/*");
async = www.Send();
while (!async.isDone)
    yield return null;
if (www.isError)
{
    Debug.Log(www.error);
}
else
{
    //yield return null; // This<-------------------
    yield return new WaitForEndOfFrame();  // OR This<-------------------
    tex = DownloadHandlerTexture.GetContent(www);   
}

虽然,我不知道这有多可靠。除非进行彻底的测试,否则我不会在商业产品中使用它。

一个可靠的解决方案是提交有关www.isDone的错误,然后不要使用www.isDone。使用yield return www.Send();直到修复此问题。

UnityWebRequest www = UnityWebRequest.GetTexture("https://0.gravatar.com/avatar/fc2beef90fad49f83d79650a10b5c030?s=256&d=identicon&r=G");
www.SetRequestHeader("Accept", "image/*");
yield return www.Send(); // This<-------------------

if (www.isError)
{
    Debug.Log(www.error);
}
else
{
    tex = DownloadHandlerTexture.GetContent(www);    
}