Search code examples
c#imageunity-game-enginetexturesaspect-ratio

Aspect ratio incorrect imported image unity editor script


I have written a script that should make sure the aspect ratio of my image stays the same when i import it into unity.
This is the script:

public class PostprocessImages : AssetPostprocessor
{
    void OnPostprocessTexture(Texture2D texture)
    {
        TextureImporter textureImporter = (TextureImporter)assetImporter;
        textureImporter.npotScale = TextureImporterNPOTScale.None;
        textureImporter.mipmapEnabled = false;
    }
}

the script is located in the Editor folder in Assets. But when i import an image i get the following result:

enter image description here

this is definitly not what i am looking for.
It should look like this:

enter image description here

But when i just go to the inspector of the imported image and change a random setting and hit apply the texture does get the proper aspect ratio back. And the settings i modified in the code. Are correct when i inspect the image. But the image doesnt have the right aspect ration..
See image below:

enter image description here

how would i go about making the image have the right aspect ratio, right from the getgo (when i import it). Because i dont want to do it manual since i have to import hundereds of images.

Let me know if something is unclear so i can clarify.


Solution

  • I don't have this issue but can think of possible fixes.

    The first thing to do is to use the OnPreprocessTexture function instead of OnPostprocessTexture. This will modify the Texture before it is imported.

    public class PostprocessImages : AssetPostprocessor
    {
        void OnPreprocessTexture()
        {
            TextureImporter textureImporter = (TextureImporter)assetImporter;
            textureImporter.npotScale = TextureImporterNPOTScale.None;
            textureImporter.mipmapEnabled = false;
        }
    }
    

    If that doesn't work, call TextureImporter.SaveAndReimport() after making the changes on the Texture so that Unity will re-import it after making the changes. This is likely to fix the issue. It's worth noting that you need a way to make sure that TextureImporter.SaveAndReimport() is called once because calling TextureImporter.SaveAndReimport() will trigger OnPreprocessTexture(). Without implementing a way to determine this, you will enter into an infinite loop of never ending texture importing. In the example below, I used a static List to implement that:

    static List<TextureImporter> importedTex = new List<TextureImporter>();
    
    void OnPostprocessTexture(Texture2D texture)
    {
        TextureImporter textureImporter = (TextureImporter)assetImporter;
    
        /*Make sure that SaveAndReimport is called once ONLY foe each TextureImporter
         There would be infinite loop if this is not done
         */
        if (!importedTex.Contains(textureImporter))
        {
            textureImporter.npotScale = TextureImporterNPOTScale.None;
            importedTex.Add(textureImporter);
            textureImporter.SaveAndReimport();
        }
    }