Guaranteeing certain Texture2D properties on files in a certain folder, 'Not Yet Compressed'

Hi Everyone,

We have a single folder in our project where we store ability icon sprites for our game. I’d like to make certain properties consistent across all files in this folder (maxTextureSize, don’t generate mipmaps, etc)

I created a class called TexturePostImporter that derives from AssetPostprocessor. By hooking into OnPostprocessTexture I can set the right properties on my TextureImporter instance and have all new images take on the right properties.

Now I’d like to be able to reimport my existing sprites and have these standard values applied to them as well. The problem is that when I do the reimport of existing sprites, I get the dreaded ‘Not yet Compressed’ message when I preview the reimported sprites. I checked my editor prefs and my ‘Compress on Import’ is indeed turned on.

Does anyone see anything obviously wrong in my sample code?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class TexturePostImporter : AssetPostprocessor
{
    private static TextureTypeInfo[] s_handlers = new TextureTypeInfo[]
    {
        new TextureTypeInfo() { pathPrefix = "Assets/Bundles/UI/Icons/Ability/", handler = HandleAbilityIcons },
    };

    void OnPostprocessTexture(Texture2D tex)
    {
        var ti = assetImporter as TextureImporter;

        var path = ti.assetPath;
        for ( int i = 0; i < s_handlers.Length; ++i )
        {
            var info = s_handlers[i];
            if ( path.ToLower().StartsWith(info.pathPrefix.ToLower()) )
            {
                info.handler(ti, tex);
               
                var existingAsset = AssetDatabase.LoadAssetAtPath<Texture2D>(ti.assetPath);
                if ( existingAsset == null )
                {
                    ti.SaveAndReimport(); // new asset, save it
                }
                else
                {
                    // this leaves the existing asset in a weird state ('Not Yet Compressed')
                    EditorUtility.SetDirty(existingAsset);
                    AssetDatabase.SaveAssets();
                }
       
                break;
            }
        }
    }
   
   
   
    private static void HandleAbilityIcons(TextureImporter importer, Texture2D tex)
    {
        importer.textureType = TextureImporterType.Sprite;
        importer.maxTextureSize = 128;
        importer.alphaIsTransparency = false;
        var defaultSettings = importer.GetDefaultPlatformTextureSettings();
        defaultSettings.maxTextureSize = 128;
    }
   
    private class TextureTypeInfo
    {
        public string pathPrefix;
        public System.Action<TextureImporter, Texture2D> handler;
    }
}

We’re on 2017.4.25f1.

Thanks in advance,
Jeff

if you modify the import settings, you have to call SaveAndReimport. this will cal the importer again, so you need to avoid infinite loops yourself

or you use Unity - Scripting API: AssetPostprocessor.OnPreprocessTexture() instead