使用 Unity RestClient 包下载图像时出现“指定的演员无效”错误

问题描述 投票:0回答:1

我尝试从 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);
    });
}
c# visual-studio unity-game-engine unityscript
1个回答
0
投票

我找到了解决方案,我很愚蠢地忽略了基本的事情。

这是代码:

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);
        });
    }
© www.soinside.com 2019 - 2024. All rights reserved.