Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Editor Workflows' started by jchowdown-asla, Oct 24, 2019.

  1. jchowdown-asla

    jchowdown-asla

    Joined:
    May 10, 2017
    Posts:
    26
    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?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class TexturePostImporter : AssetPostprocessor
    7. {
    8.     private static TextureTypeInfo[] s_handlers = new TextureTypeInfo[]
    9.     {
    10.         new TextureTypeInfo() { pathPrefix = "Assets/Bundles/UI/Icons/Ability/", handler = HandleAbilityIcons },
    11.     };
    12.  
    13.     void OnPostprocessTexture(Texture2D tex)
    14.     {
    15.         var ti = assetImporter as TextureImporter;
    16.  
    17.         var path = ti.assetPath;
    18.         for ( int i = 0; i < s_handlers.Length; ++i )
    19.         {
    20.             var info = s_handlers[i];
    21.             if ( path.ToLower().StartsWith(info.pathPrefix.ToLower()) )
    22.             {
    23.                 info.handler(ti, tex);
    24.                
    25.                 var existingAsset = AssetDatabase.LoadAssetAtPath<Texture2D>(ti.assetPath);
    26.                 if ( existingAsset == null )
    27.                 {
    28.                     ti.SaveAndReimport(); // new asset, save it
    29.                 }
    30.                 else
    31.                 {
    32.                     // this leaves the existing asset in a weird state ('Not Yet Compressed')
    33.                     EditorUtility.SetDirty(existingAsset);
    34.                     AssetDatabase.SaveAssets();
    35.                 }
    36.        
    37.                 break;
    38.             }
    39.         }
    40.     }
    41.    
    42.    
    43.    
    44.     private static void HandleAbilityIcons(TextureImporter importer, Texture2D tex)
    45.     {
    46.         importer.textureType = TextureImporterType.Sprite;
    47.         importer.maxTextureSize = 128;
    48.         importer.alphaIsTransparency = false;
    49.         var defaultSettings = importer.GetDefaultPlatformTextureSettings();
    50.         defaultSettings.maxTextureSize = 128;
    51.     }
    52.    
    53.     private class TextureTypeInfo
    54.     {
    55.         public string pathPrefix;
    56.         public System.Action<TextureImporter, Texture2D> handler;
    57.     }
    58. }
    59.  
    We're on 2017.4.25f1.


    Thanks in advance,
    Jeff
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559