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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

ResourcesLoad and File.ReadOpen

Discussion in 'Scripting' started by -JoSeM-, May 6, 2018.

  1. -JoSeM-

    -JoSeM-

    Joined:
    Jul 8, 2014
    Posts:
    9
    Hello...
    I've been searching for an entire day and I found no solution.
    I am trying to get the real or native width and height from my PNG files. As soon as the PNG files are imported to unity, they width and height are converted to nearest power 2. That's perfectly fine, that makes the images weight less.
    If I do Resources.Load(image), I get the Image and width&heigth on power of 2. To get the native width&height I run a script I found that could load the native PNG width and height. By loading the images with File.OpenRead and then convert it to Binary, I could get the native width&height.

    Example:
    1)
    Code (CSharp):
    1. Texture texture = Resources.Load(path, typeof(Texture)) as Texture;
    if I do texture.width I would obtain for example 256 (power of 2)
    2)
    Code (CSharp):
    1. BinaryReader binaryReader = new BinaryReader(File.OpenRead("Resources/" + path));
    But then, if I run the binary code I would get the native width before compression (314, no power of 2).

    Ok, that works OK! Atleast on Editor... On a build, it prints an error. Because the path on resources don't exist. The files on resources get compressed and there isn't a clear path.

    How can I solve the problem?
    I've tried to Resources.Load as TextAsset, then get the bytes by reading it as PNG but, the loaded width & height are the power 2 ones.

    Greetings and thank you.
     
    Last edited: May 6, 2018
  2. -JoSeM-

    -JoSeM-

    Joined:
    Jul 8, 2014
    Posts:
    9
    The most cheaper solution found is... duplicate the files, make them "cookie" with low quality and size, ~10Kb, to get an approach of the native image size by resources.load them