Error m_InstanceID != 0 when downloading texture from the server

时光怂恿深爱的人放手 提交于 2019-12-24 10:38:15

问题


I'm getting this error in Unity 5.4 when trying to download the texture from the server.

Here is the code (the link should work):

     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);    // <-------------------
     }

The error looks like this:

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

回答1:


This is a bug. It happens when www.isDone or async.isDone is used with DownloadHandlerTexture.

The work around is to wait for another frame with yield return null; or yield return new WaitForEndOfFrame() before calling 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);   
}

Although, I don't know how reliable this is. I wouldn't use this in a commercial product unless a thorough test is performed.

A reliable solution is to file for bug about www.isDone, then don't use www.isDone. Use yield return www.Send(); until this is fixed.

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);    
}


来源:https://stackoverflow.com/questions/39628951/error-m-instanceid-0-when-downloading-texture-from-the-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!