我正在使用 C# 脚本在 Unity 中从 Internet 加载图片。有时一切正常,有时会报错,说纹理尚未加载。什么问题,我不明白,不同的图片稳定性不同。而且它不取决于重量。
public IEnumerator LoadImage()
{
_img = _avatar.GetComponent<Image>();
if (_url == "")
{
Debug.Log("пусто");
}
else
{
UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(_url);
yield return webRequest.SendWebRequest();
if (webRequest.isDone == true)
{
Texture texture = ((DownloadHandlerTexture)webRequest.downloadHandler).texture;
Debug.Log(webRequest.error);
_img.sprite = Sprite.Create((Texture2D)texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
}
else
{
Debug.Log(webRequest.error);
}
}
}
这里是错误的文本:
InvalidOperationException:纹理尚未完成下载 UnityEngine.Networking.DownloadHandlerTexture.InternalGetTexture () (at <8493ecd1d0614bc382e5fcf3adc16a08>:0)
UnityEngine.Networking.DownloadHandlerTexture.get_texture () (at <8493ecd1d0614bc382e5fcf3adc16a08>:0)
DataBase+d__27.MoveNext () (at Assets/Scripts/DataBase.cs:161)
UnityEngine.SetupCoroutine.InvokeMoveNext(System.Collections.IEnumerator 枚举器,System.IntPtr returnValueAddress)(在:0)