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

Textures, Base64, Encoding Decoding

Discussion in 'Scripting' started by Conflei, Apr 15, 2014.

  1. Conflei

    Conflei

    Joined:
    Apr 21, 2013
    Posts:
    44
    Hello everyone, i'm working on a game and i need to code an decode an texture from a sprite.

    I'm using System.Convert but it's not working, here's the code:

    For encode:

    Code (csharp):
    1.  
    2.  
    3. string base64Img;
    4.  
    5. WWW www = new WWW (url);
    6.  
    7.         base64Img = System.Convert.ToBase64String (www.texture.EncodeToPNG());
    8.  
    9.  
    10.  
    For decode:

    Code (csharp):
    1.  
    2.  
    3.                 byte[] Bytes = System.Convert.FromBase64String (base64Img);
    4.         Texture2D tex = new Texture2D (500, 700);
    5.         tex.LoadImage (Bytes);
    6.         Rect rect = new Rect(0, 0, tex.width, tex.height);
    7.         sprite = Sprite.Create (www.texture, rect, new Vector2() , 100f);
    8.  
    9.  
     
  2. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    1) What is it doing instead of working?
    2) How crucial is it to encode it as a string instead of cutting out the middle-man and just assigning www.texture directly to tex? Are you playing with the contents of base64Img to try to glitch it out or something?

    I notice your Sprite.Create is partly bypassing the middle-man by referencing www.texture directly instead of tex, so I'm curious.

    It would also be worth it to test passing www.texture.EncodeToPNG() directly to tex,LoadImage() to confirm if the string encoding is the problem or something else.
     
  3. Conflei

    Conflei

    Joined:
    Apr 21, 2013
    Posts:
    44
    Dosnt show anything.

    It's crucial cos it's not a standalone game, it's multiplayer with a server.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I've done this before and it's worked fine. Are you loading a file off the disk that you saved in another application or are you trying to write a file in Unity to disk and then reload it via WWW?
     
  5. Conflei

    Conflei

    Joined:
    Apr 21, 2013
    Posts:
    44
    I'm loading of the disk and i want to encode it to load via decode.

    Phone1:
    -Load from disk
    -Encode to string
    -Send String to phone 2

    phone2:
    -decode string
    -show image
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Why are you bothering with encoding strings? Just send the data directly.

    --Eric
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    My guess is that he's doing it via RPC which doesn't accept a byte array but does accept a string.
     
  8. kk99

    kk99

    Joined:
    Nov 5, 2013
    Posts:
    81
    I have been struggling with this also a pretty long time and I guess you are doing the same mistake


    byte[] Bytes = System.Convert.FromBase64String(BLACK_MAGIC_STRING);
    this function is taking the BASE64 string without any prefix

    so THIS IS WRONG
    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAgAElEQVR4Xty9B7hlV3Xn+d/73PuqXuWonHNAIiOCyBnTYMA27cCME3gcuu1vPO7xTDc27a+/nmnbM9NjG...

    THIS IS CORRECT
    iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAgAElEQVR4Xty9B7hlV3Xn+d/73PuqXuWonHNAIiOCyBnTYMA27cCME3gcuu1vPO7xTDc27a+/nmnbM9NjG...



    Cheers