我尝试从 URL 下载图像,但使用 Rest Client 包时出现“指定的转换无效”错误。这是我的代码,请纠正我做错的事情:
public void DownloadImage(string fileUrl, string name)
{
RestClient.Get(new RequestHelper {
Uri = fileUrl,
// DownloadHandler = new DownloadHandlerFile(imagePath + filename),
Method = "GET",
DefaultContentType = true,
EnableDebug = true,
}).Then(res =>
{
Texture2D tex = ((DownloadHandlerTexture) res.Request.downloadHandler).texture;
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(tex.width / 2, tex.height / 2));
if (img != null)
{
img.overrideSprite = sprite;
img.preserveAspect = true;
}
}).Catch(err => {
Debug.Log(err.Message);
Debug.Log("url: " + fileUrl);
});
}
我找到了解决方案,我很愚蠢地忽略了基本的事情。
这是代码:
public void DownloadImage(string fileUrl, string name)
{
RestClient.Get(new RequestHelper {
Uri = fileUrl,
DownloadHandler = new DownloadHandlerTexture(true),
Method = "GET",
DefaultContentType = true,
EnableDebug = true,
}).Then(res =>
{
Texture2D tex = ((DownloadHandlerTexture) res.Request.downloadHandler).texture;
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(tex.width / 2, tex.height / 2));
if (img != null)
{
img.overrideSprite = sprite;
img.preserveAspect = true;
}
}).Catch(err => {
Debug.Log(err.Message);
Debug.Log("url: " + fileUrl);
});
}