Search Unity

Texture to byte array and then back to texture.

Discussion in 'Getting Started' started by WahoPlay, Jun 12, 2020.

  1. WahoPlay

    WahoPlay

    Joined:
    May 31, 2016
    Posts:
    5
    Hello, i am trying to convert a texture to byte array and then to string and then save it on firebase and then again fetching it and converting it to texture.
    But right now it is not even working locally. i am trying to convert a texture to byte array using GetRawTextureData and loading it using loadrawtexture data but that is also giving me some random shaded colors.

    Thanks in advance.


    Code (CSharp):
    1. void TheTest()
    2.     {
    3.         byte[] bArray = test.GetRawTextureData();
    4.         Texture2D tex = new Texture2D(2, 2, TextureFormat.RGBA32, false);
    5.        
    6.         tex.LoadRawTextureData(bArray);
    7.         tex.Apply();
    8.         abc.texture = tex;
    9.     }
     
    MaxLohMusic likes this.
  2. smt_juangs

    smt_juangs

    Joined:
    Mar 25, 2021
    Posts:
    1
    I'm facing the same problem here. Did you get to solve it by any chance?
    In my case, I need a byte array from a texture that is being dynamically painted, then feed it to EasyAR.
    I can pass an arbitrary byte array to EasyAR and it represents it correctly. But when I retrieve the byte array from the texture, it always returns me the same shade of gray. From other posts I tried to use Apply() and call it after the Renderer Update, but without any luck.
     
    MaxLohMusic likes this.
  3. ManjuYadav

    ManjuYadav

    Joined:
    Dec 21, 2019
    Posts:
    2
    void Start()
    {
    byte[] bArray = test.GetRawTextureData();
    string texAsString = Convert.ToBase64String(bArray);
    Texture2D tex = new Texture2D(2, 2, TextureFormat.RGBA32, false);

    tex.LoadRawTextureData(bArray);
    tex.Apply();
    abc.texture = tex;
    }
     
    acevere likes this.
  4. MidlightDDK

    MidlightDDK

    Joined:
    Apr 17, 2021
    Posts:
    1
    Hey, I had the same problem today and for me, the problem was the format.
    Instead of:
    Code (CSharp):
    1. Texture2D tex = new Texture2D(2, 2, TextureFormat.RGBA32, false);
    Try:
    Code (CSharp):
    1. Texture2D tex = new Texture2D(2, 2, test.format, false);
    The goal is to have the same format for both textures, which isn't necessarily RGBA32 as you might think depending on your import settings. If you want it to be necessarily RGBA32 then, you have to check it with a if statement and throw and exception if that's not the case.
    Good luck to you guys!
     
  5. jfrs20

    jfrs20

    Joined:
    Jul 8, 2020
    Posts:
    13
    AFAICT, the texture to be loaded in LoadRawTextureData(...) must be created with the same width and height as it was for .GetRawTextureData(). The usual "2, 2" does not work here.