proyecto26 / RestClient

🦄 A Promise based REST and HTTP client for Unity 🎮
https://assetstore.unity.com/packages/slug/102501
MIT License
1.24k stars 173 forks source link

Specified cast is not valid. #243

Closed minhaj-inspiredsquare closed 1 year ago

minhaj-inspiredsquare commented 1 year ago

Hi, I'm trying to download an image from the URL but it gives me the "Specified cast is not valid" error. Here is my code please correct me on what I'm doing wrong:

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);
        });
    }
minhaj-inspiredsquare commented 1 year ago

Found solution:

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