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

Download Hexadecimal based PNG and convert to byte?

Discussion in 'Editor & General Support' started by yyefet81, Jun 9, 2020.

  1. yyefet81

    yyefet81

    Joined:
    Jul 14, 2019
    Posts:
    6
    How can I download from my server and convert hexadecimal based PNG to byte (png file) used for texture?

    Example output from server:
    b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01,\x00\x00\x00\xaf\x08\x06\x00\x00\x00G.\xe3\xb7\x00\x00\x1c\xfdIDATx^\xed\x9dw...' <redacted>

    I tried 3 or 4 ways but struggling a bit.

    Anyone have any examples?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. public class MyBehaviour : MonoBehaviour {
    5.     void Start() {
    6.         StartCoroutine(GetTexture());
    7.     }
    8.     IEnumerator GetTexture() {
    9.         UnityWebRequest www = UnityWebRequestTexture.GetTexture("http://www.my-server.com/image");
    10.         yield return www.SendWebRequest();
    11.  
    12.         if(www.isNetworkError || www.isHttpError) {
    13.             Debug.Log(www.error);
    14.         }
    15.         else {
    16.             Texture myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
    17.         }
    18.     }
    19. }
     
  2. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    506
    Hi @yyefet81,

    One thing that I noticed is that URL doesn't have the PNG extension.

    Also, you need to use `DownloadHandlerTexture.GetContent` on your `UnityWebRequest` in order to have the Texture.

    You can find more information about it here: https://docs.unity3d.com/ScriptReference/Networking.DownloadHandlerTexture.GetContent.html

    Also, you can check a working example of the approach that probably you should be using here: https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequestTexture.GetTexture.html


    Good luck with it!
     
  3. yyefet81

    yyefet81

    Joined:
    Jul 14, 2019
    Posts:
    6
    Thanks Diego!

    I should have mentioned that the PNG image is being stored in Redis, so we are only returning the Hex string from that hypothetical url.
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,003
    The extension is secondary unless Unity expects that. But are you sure that you're giving out the proper header on the webserver? content-type: image/png
     
  5. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    506
    That's right, I mentioned this explicitly for him to check that the URL he's using is the right one ;)
     
  6. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    506
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,653
    UnityWebRequestTexture.GetTexture requires the downloaded bytes to be image file, so it has to be an actual PNG, not it's textual representation.
    So, it looks you have to either store your image elsewhere (where you could download it raw) or do the conversion yourself. In the later case use UnityWebRequest.Get, get data from download handler and do the manual conversion into bytes. Then use ImageConversion.LoadImage to convert bytes into texture.