Skip to content

Instantly share code, notes, and snippets.

@nicloay
Last active April 27, 2019 05:10
Show Gist options
  • Save nicloay/a6b2f3427a5bb67024fc to your computer and use it in GitHub Desktop.
Save nicloay/a6b2f3427a5bb67024fc to your computer and use it in GitHub Desktop.
Unity3d Automatically fix texture size on import (very useful if you have big textures which has bigger size than default 1024 or 2048 value)
// reflexion method posted by numberkruncher
// here http://forum.unity3d.com/threads/getting-original-size-of-texture-asset-in-pixels.165295/
public class AtlasTexturePostprocessor : AssetPostprocessor {
public const int PixelsPerUnit = 100;
void OnPreprocessTexture(){
Texture2D tex = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Texture2D )) as Texture2D;
TextureImporter importer = assetImporter as TextureImporter;
int textureHeight;
FixTextureSize(tex,importer, out textureHeight);
}
static int[] textureSizes = new int[] {
32,
64,
128,
256,
512,
1024,
2048,
4096
};
void FixTextureSize (Texture2D tex, TextureImporter importer, out int textureRealHeigh)
{
int width, height, max;
GetImageSize(tex,out width, out height);
textureRealHeigh = height;
max = Mathf.Max(width, height);
int size = 1024; //Default size
for (int i = 0; i < textureSizes.Length; i++) {
if (textureSizes[i] >= max){
size = textureSizes[i];
break;
}
}
importer.maxTextureSize = size;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment