Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How To get A correct conversion from image to Text To image ?

Discussion in 'Scripting' started by blackant, Mar 30, 2022.

  1. blackant


    Jun 18, 2009
    I did some test to transfer values from a texture to another, and it works perfectly, if you transfer byte [] to another one.

    But when want to read the values and use it in a string, it doesn't work the same way.
    I use this code, what should be modified ?

    Code (CSharp):
    1. using UnityEngine;
    3. public class GetTextureData : MonoBehaviour
    4. {
    5.     public Texture2D TexRef;
    6.     private string DatasText;
    7.     private Texture2D ImageRes;
    8.     private static byte[] dataBytes;
    9.     private void OnGUI()
    10.     {
    11.         GUILayout.BeginHorizontal();
    12.         GUILayout.TextArea(DatasText);
    13.         GUILayout.Space(10);
    14.         GUI.DrawTexture(new Rect(Screen.width*.5f, Screen.height*.5f, TexRef.width, TexRef.height), ImageRes, ScaleMode.ScaleToFit,TexRef.mipmapCount>1) ;
    15.         GUILayout.EndHorizontal();
    16.     }
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.          dataBytes = TexRef.GetRawTextureData();
    22.         for (int i=0; i<dataBytes.Length;i++ )
    23.         {
    24.             string datas = dataBytes[i].ToString();
    25.             DatasText += datas+",";
    26.         }
    27.         ImageRes = new Texture2D(2, 2);
    28.         ImageRes.LoadRawTextureData(dataBytes);
    29.         ImageRes.Apply();
    30.     }
    31. }
    NB: In my case if i replace dataBytes variable with
    Code (CSharp):
    1.  ImageRes.LoadRawTextureData(TexRef.GetRawTextureData());
    the result is as bad as the first technic...
    Last edited: Mar 30, 2022
  2. StarManta


    Oct 23, 2006
    The bytes probably include things that don't convert well into a character. If you convert the byte to an integer before converting it to a string that would probably be more reliable.

    You also don't show here the string-to-Texture conversion process which seems like it could also have lots of potential problems, like parsing out the commas for example.

    However it really seems like what you're doing here is a bad idea overall and I feel compelled to ask, why? This is like asking for help in fixing your shotgun so you can shoot yourself in the leg.
  3. MelvMay


    Unity Technologies

    May 24, 2013
  4. blackant


    Jun 18, 2009
    I'm not using a shotgun, it's too slow ^^

    I'm doing this to understand how behave the whole system of conversion between data.
    It may be useful in some ways later to explain why the things are like they are and why we do things like we do.
    Or it may also be useful as artistic creations like the result of this array in string looks cool when it starts representing something like the screenshot.
    We can clearly see the reflection of the image, but this is not perfect. And to be perfect it has to be understood, which in my case is clearly not

    Attached Files: