Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question UnityWebRequest shows an "?" when downloading an image from a server

Discussion in 'Scripting' started by Martincas, Oct 5, 2022.

  1. Martincas

    Martincas

    Joined:
    Jul 10, 2017
    Posts:
    4
    Hi all!

    I've been trying to download some jpgs from a server to show them in unity but there are some images (the most of them) that are downloaded like this:


    This honestly is working for some other pictures.
    The images are in jpg format and the download link is working perfectly since i manually downloaded the image from that link.

    This is the code i'm using:

    Code (CSharp):
    1. private static IEnumerator GetImageCoroutine(string url, Action<string> onError, Action<Texture2D> onSuccess, Dictionary<string, string> _header = null)
    2.     {
    3.         using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(url))
    4.         {
    5.  
    6.             yield return www.SendWebRequest();
    7.             if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
    8.             {
    9.                 onError(www.error);
    10.             }
    11.             else
    12.             {
    13.              
    14.                 DownloadHandlerTexture _texture = www.downloadHandler as DownloadHandlerTexture;
    15.                 onSuccess(_texture.texture);
    16.              
    17.             }
    18.  
    19.         }
    20.     }

    and for setting the images i'm doing this:

    Code (CSharp):
    1.   foreach (Gallery imgData in _images)
    2.         {
    3.             ImageRequester.GetImage(imgData.url, Error, (Texture2D _texture) => {
    4.                 Sprite _sprite = Sprite.Create(_texture, new Rect(0, 0, _texture.width, _texture.height), new Vector2(.5f, .5f), 10);
    5.                 _spriteList.Add(_sprite);
    6.  
    7.                 if (imgData.position == "tag")
    8.                 {
    9.                     mainIndex = counter;
    10.                     m_TagImage.color = Color.white;
    11.                     m_TagImage.sprite = _sprite;
    12.                     m_TagImage.preserveAspect = true;
    13.                     m_TagImage.GetComponent<Button>().onClick.AddListener(() => ExpandImage(mainIndex));
    14.                     _tagImageBcgrnd.sprite = _sprite;
    15.                     _tagImageBcgrnd.preserveAspect = true;
    16.                     _loadingTagPanel.SetActive(false);
    17.                     counter++;
    18.                     return;
    19.                 }
    20.  
    21.                 if (imgData.position == "main")
    22.                 {
    23.                     tagIndex = counter;
    24.                     m_MainImage.color = Color.white;
    25.                     m_MainImage.sprite = _sprite;
    26.                     m_MainImage.preserveAspect = true;
    27.                     _mainImageBcgrnd.sprite = _sprite;
    28.                     _mainImageBcgrnd.preserveAspect = true;
    29.                     m_MainImage.GetComponent<Button>().onClick.AddListener(() => ExpandImage(tagIndex));
    30.                     _loadingMainPanel.SetActive(false);
    31.                     counter++;
    32.                     return;
    33.                 }
    34.  
    35.                 GameObject _thumb = Instantiate(m_thumbPrefab, m_thumbContentPositions);
    36.                 Transform _img = _thumb.transform.Find("Image");
    37.                 Image _bckground = _thumb.transform.Find("ImageBcground").GetComponent<Image>();
    38.                 _bckground.sprite = _sprite;
    39.                 _bckground.preserveAspect = true;
    40.                 _img.gameObject.GetComponent<Image>().sprite = _sprite;
    41.                 _img.gameObject.GetComponent<Image>().preserveAspect = true;
    42.                 Button _imgButton = _img.gameObject.GetComponent<Button>();
    43.                 _imgButton.onClick.AddListener(() => ExpandImage(_sprite));
    44.  
    45.  
    46.                 _thumbList.Add(_thumb);
    47.                
    48.                 counter++;
    49.                
    50.             });
    51.         }
    Thanks in advance for any help.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

    https://forum.unity.com/threads/using-unity-for-subscription-lists.1070663/#post-6910289

    https://forum.unity.com/threads/unity-web-request-acting-stupid.1096396/#post-7060150

    And setting up a proxy can be very helpful too, in order to compare traffic:

    https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity

    Keep in mind that these images might SEEM to be JPEGs to you but actually be some other wonky newfangled image format that your browser can handle but that Unity cannot handle. Try getting the binary data from the server yourself, including it into the Unity build as a
    .bytes
    file and loading it to see if Unity can turn it into a Texture2D or not.

    https://docs.unity3d.com/Manual/class-TextAsset.html

    https://docs.unity3d.com/ScriptReference/TextAsset.html
     
    Martincas and Bunny83 like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    Right, this is a quite common issue. Just because someone may have renamed the file to jpg or jpeg doesn't make the file a jpg ;). Browsers are extremely fault tolerant. They usually don't care about the file extension. They may care about the mime type but usually they just analyse the file and see which loader works.

    So I would recommend to test your code first with an image that you know for a fact is a jpg. If the image is not a secret you could also share it with us so we can have a look at it. Though that's up to you.
     
    Kurt-Dekker likes this.
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
    also, what resolution are those images that wont load correctly?
     
  5. Martincas

    Martincas

    Joined:
    Jul 10, 2017
    Posts:
    4
    Oh, I will try it, but i noticed that the pictures that loaded have the color representation as sRGB and the ones that don't work has the color representation as uncalibrated. It has something to do?
     
  6. Martincas

    Martincas

    Joined:
    Jul 10, 2017
    Posts:
    4
    are different resolutions, i think one is like 4496 x 3000 (this image is downloaded and shown without any problem) and other one is 1906x1272
     
  7. Martincas

    Martincas

    Joined:
    Jul 10, 2017
    Posts:
    4
    Ok, the problem was the software we are using to export the jpg, it seems that it change something on the jpg format, we use an online converter to see if that fixes the problem and it worked perfectly, thanks for the reply!
     
    Kurt-Dekker likes this.
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    Technically speaking almost no program actually supports the full JPEG standard and what we usually understand by jpeg is actually the JFIF format. If someone is interested in how jpeg works, I can highly recommend the jpeg playlist of computerphile. I've linked the second video as it starts with jpeg. The first is only about color spaces. Though if you have some time, go watch it :)

    Even the JFIF format allows all sorts of different formats and Unity may not support all variations.
     
    Martincas and Kurt-Dekker like this.