0
votes

I am trying to fetch a image texture from

IEnumerator GetRequest1(string uri)
{
    using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
    {
        yield return webRequest.SendWebRequest();
        img = ((DownloadHandlerTexture)webRequest.downloadHandler).texture;      
    }
}

and passing it to a GUI

void OnGUI()
{
    GUILayout.BeginArea(new Rect(100, 50, 500, 500));
    GUILayout.Label(img);
    GUILayout.EndArea();
}

on it should have worked previous days it was, today with some modifications its hitting error ,tried this and much more, but could not get helped. It would be great if I get a hint. thank you

i am using same method somewere else but thats perfect , in here its hitting error as

InvalidCastException: Specified cast is not valid. gettiles+d__25.MoveNext () (at Assets/Scripts/gettiles.cs:111)UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

an InvalidCastException error Hits in case where the actual type of the data doesn't match the type which was passed But here it is sprite to sprite

specificimage.sprite = Sprite.Create(((DownloadHandlerTexture)webRequest.downloadHandler).texture, new Rect(0, 0, 250,250).texture.height), new Vector2(0.5f, 0.5f));

2
what type is img variable?G. Threepwood
Texture2D img; ofCourse,akhilesh
try to use UnityWebRequest request = UnityWebRequestTexture.GetTexture(url)Mohammed Sajid
i am using this currentlyakhilesh

2 Answers

2
votes

Try this:

using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(uri))
{
       yield return uwr.SendWebRequest();

       if (uwr.isNetworkError || uwr.isHttpError)
       {
           Debug.Log(uwr.error);
       }
       else
       {
           img =  DownloadHandlerTexture.GetContent(uwr);
           specificimage.sprite =Sprite.Create(img, new Rect(0, 0, img.width, img.height), new Vector2(0.5f, 0.5f));
       }
}
-1
votes

Problem was not with DownloadHandlerTexture problem was the image i was getting "TileFusion1".png insted i should get TileFusion1.png simple but irritating one, HTTP/1.1 403 Forbidden was hitting because of this