Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Image From URl

Discussion in 'Scripting' started by mholmes, Feb 4, 2021.

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    Trying to get an image from url to a scene. Getting a error I've never seen before. Not sure what I'm doing wrong here:

    Code (CSharp):
    1. IEnumerator ImageWebRequest(string uri)
    2.     {
    3.         try
    4.         {
    5.             //string json = JsonConvert.SerializeObject(obj);
    6.             var client = new HttpClient();
    7.             var webRequest = WebRequest.Create(uri);
    8.  
    9.             webRequest.Method = "GET";
    10.             webRequest.ContentType = "application/json";
    11.  
    12.             //using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
    13.             //{
    14.             //    streamWriter.Write();// json);
    15.             //}
    16.  
    17.             var httpResponse = (HttpWebResponse)webRequest.GetResponse();
    18.             using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    19.             {
    20.                 var result = streamReader.ReadToEnd();
    21.                 _EventTexture = JsonConvert.DeserializeObject<DownloadHandlerTexture>(result).texture;
    22.             }
    23.  
    24.             _EventImage.texture = _EventTexture;
    25.         }
    26.         catch (Exception ex)
    27.         {
    28.             Debug.Log(ex);
    29.             Debug.Log("Failed To Get News");
    30.         }
    31.  
    32.         yield return _EventImage.texture;
    33.     }
    error:
    Coroutine continue failure

    Get error on this line:
    Code (CSharp):
    1. _EventTexture = JsonConvert.DeserializeObject<DownloadHandlerTexture>(result).texture;
    Not sure what I'm doing wrong.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
  3. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    That works, so how do you control the size of the image? I'm using a raw image currently can I use a regular image?
     
  4. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    I altered my code. I'm going to try and use a Image instead of a raw image but now I get an error:

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using UnityEngine.UI;
    5.  
    6. public class WBGEvents : MonoBehaviour
    7. {
    8.     //public Text _EventTitle;
    9.     //public Text _EventDate;
    10.     //public Text _EventTime;
    11.     //public Text _EventHost;
    12.     //public Text _EventLocation;
    13.     //public Text _EventDescription;
    14.     //public RawImage _EventImage;
    15.  
    16.     //public Renderer thisrenderer;
    17.     private string url = "https://ibb.co/jkf8Yx5";
    18.     //private Texture _EventTexture;
    19.     public Image _EventTexture;
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         //StartCoroutine(LoadImageFromURL());
    25.         //StartCoroutine(DownloadImage(url));
    26.         //StartCoroutine(ImageWebRequest(url));
    27.         StartCoroutine(GetTexture());
    28.     }
    29.  
    30.     IEnumerator GetTexture()
    31.     {
    32.         UnityWebRequest www = UnityWebRequestTexture.GetTexture("http://www.my-server.com/image.png");
    33.         yield return www.SendWebRequest();
    34.  
    35.         if (www.isNetworkError || www.isHttpError)
    36.         {
    37.             Debug.Log(www.error);
    38.         }
    39.         else
    40.         {
    41.             Texture myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
    42.  
    43.             _EventTexture.GetComponent<SpriteRenderer>().sprite.texture = myTexture;
    44.         }
    45.     }
    Errors on this line:
    Code (CSharp):
    1. _EventTexture.GetComponent<SpriteRenderer>().sprite.texture = myTexture;
    Property or indexer cannot be assigned to -- it is read only

    Need a way to use Image. Ideas?
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Image doesn't have a spriteRenderer. It's just a sprite. So something like this is how I assign a downloaded texture to an Image.

    Code (CSharp):
    1. public Image targetImage;
    2. Sprite newSprite = Sprite.Create(imageTex, new Rect(0, 0, imageTex.width, imageTex.height), new Vector2(.5f, .5f));
    3. targetImage.sprite = newSprite;
    4.  
     
    dfred91 and Bunny83 like this.
  6. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    Works thank you. Sorry did not know what Image Text was for a second. Figure it out its Image Texture.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Yeah, that's just the texture. Sprite.Create takes a texture and converts it into a sprite to use.
     
  8. BAIZOR

    BAIZOR

    Joined:
    Jul 4, 2013
    Posts:
    112
    Unity-ImageLoader
    I made the package for loading images from remote or local destinations. It already has integrated two levels of the cache system. Each cache level is configurable (enable/disable).

    Usage
    Code (CSharp):
    1. var sprite = await ImageLoader.LoadSprite(imageURL);
    Installation
    Code (CSharp):
    1. openupm add extensions.unity.imageloader
     
    hagjay, dogugzm and teairis like this.