- Home /
Get the real texture size?
I have some texture set to nearest power of 2. So if I have a 910x400, it will be scale to 1024x512.
Aspect ratio was 2.275:1 is now 2:1
It's possible to get the real size after the import? I want the original aspect ratio.
I can do it with System.Drawing.Bitmap and load the original file but I want to know if it's possible with only Unity.
Answer by TheCatProblem · Feb 04, 2015 at 06:48 PM
It's possible to turn off automatic scaling of textures with dimensions that aren't powers of two. I don't know how Unity handles this under the hood, but I assume it would preserve the actual size of the image (e.g., when querying texture dimensions). Of course, it's better to use textures with power-of-two dimensions if possible.
To disable scaling, open the texture import settings window, set Texture Type to Advanced and change Non Power of 2 from To nearest to None.
I want to keep the scaling, but I want to adjust the aspect ratio correctly whe I use the texture
As far as I know there isn't an "official" way to retrieve the size before scaling. However, you might try the approach discussed in this forum thread: Getting original size of texture asset in pixels.
I prefer to use System.Drawing.Bitmap without using reflection with the importer. In both cases we need to store that information somewhere and use it later. Thanks anyways!
Answer by icelanguage · Sep 16, 2019 at 04:45 AM
public static void GetTextureRealWidthAndHeight(TextureImporter texImpoter, ref int width, ref int height)
{
System.Type type = typeof(TextureImporter);
System.Reflection.MethodInfo method = type.GetMethod("GetWidthAndHeight", BindingFlags.Instance | BindingFlags.NonPublic);
var args = new object[] { width, height };
method.Invoke(texImpoter, args);
width = (int)args[0];
height = (int)args[1];
}
Your answer
Follow this Question
Related Questions
Editor class "Texture Importer" question (applying settings to multiple texture assets). 2 Answers
Importing FBX with textures 1 Answer
How to stop gradient banding? 2 Answers
How to change the default import settings for textures 1 Answer
All textures constantly changes "Texture Type" to "Advanced" leaving weird light issues 2 Answers