Search Unity

Load img and get its byte code

Discussion in 'Scripting' started by nafasso, Jun 23, 2015.

  1. nafasso

    nafasso

    Joined:
    Dec 3, 2013
    Posts:
    22
    Hello everybody,

    here is my problem:
    I try to load an image at run time in order to get its byte code and then after convert it in float. So I tried to follow the Doc process but they do it using the LoadImage which take a byte array as parameter so that is not what I am looking for.

    here is my code but when I play the scene I get an error :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4.  
    5. public class LoadXCoord : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.         Texture2D pngX = new Texture2D(2,2,TextureFormat.DXT5,false);
    11.  
    12.         pngX = (Texture2D) Resources.Load("p1_x.png") as Texture2D;
    13.  
    14.         Debug.Log (pngX.name);
    15.  
    16.         byte[] bytes = pngX.EncodeToPNG();
    17.    
    18.     }
    19.  
     
  2. nafasso

    nafasso

    Joined:
    Dec 3, 2013
    Posts:
    22
    ok it seems like I found another way to do it.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4.  
    5. public class LoadXCoord : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.         byte[] bytes = File.ReadAllBytes(Application.dataPath + "/p1_x.png");
    11.  
    12.         Debug.Log (bytes.Length);
    13.  
    14.         File.WriteAllBytes(Application.dataPath + "/../pnGXX.png", bytes);
    15.    
    16.     }
    17.  
    18. }
    still don't understand what is wrong with the first way so if someone can help me to understand I would be very thankful.
     
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    You don't need the byte code if you have the texture itself.

    Make sure that you are loading the correct texture from resources. I mean that it must be in a folder called resources for it to work, and the name matches. Here is how it works.

    Will give that exception if name is null or pngX is null and seeing as it probably doesn't exists pngX is null.

    This is not the byte array you are looking for anyway. It has other info that you don't need.

    After you have the texture from resources you can use GetPixels on it to get the info you want which will be a Color array, which is the floats you want.
     
  4. nafasso

    nafasso

    Joined:
    Dec 3, 2013
    Posts:
    22
    I see I see.
    So yes first of all I loaded the texture from a folder named Resources, I know this process.
    actually I was looking for the byte code behind this image to regroup it into a float array each 4 elements because the texture is just data that I need for my program but if you tell me that the GetPixels does the same thing it will simplify my task a lot :)

    Thank you for you lights.