Quick post today — I’m knee-deep in UI tomfoolery right now and will have something consequential to post on regarding that tomorrow. This one is about an AssetPostProcessor that I cobbled together to improve my sprite importing workflow in Unity.

The problem: Unity’s default methods for importing texture assets are, like many things for Unity, made for 3D games first. In order to get my sprites to have a consistent feel and work with my camera ratio to render pixel-perfectly, I had to manually set a few options: pixels per unit had to be 32 (16 before my resolution upgrade), filter mode to “Point” and compression to “None.” This wasn’t a big problem when I was working with prototype assets, but it was starting to get on my nerves as I began to create production-ready art for UI buttons and character sprites.

I found a (partial) solution in this Unity forums answer and modified it to fit my needs; however, once imported, the changes needed to be manually applied (which was very annoying).

I then found that I could use assetImporter.SaveAndReimport() to force-apply the changes. Now, whenever I save a new sprite texture into my assets, it defaults to my requirements. This should save a lot of time.

AssetPostprocessor Code

Here’s my code if you’d like to use it for yourself. Add it to a script in an Assets/Editor folder and it should work automatically.

using UnityEngine; using UnityEditor; public class SpritePostProcessor : AssetPostprocessor { void OnPostprocessTexture(Texture2D texture) { TextureImporter texture_importer = assetImporter as TextureImporter; //Change these to whatever you want for your defaults texture_importer.textureCompression = 0; texture_importer.filterMode = FilterMode.Point; texture_importer.spritePixelsPerUnit = 32; Object asset = AssetDatabase.LoadAssetAtPath(texture_importer.assetPath, typeof(Texture2D)); if (asset) { EditorUtility.SetDirty(asset); } else { //Change these to whatever you want for your defaults texture_importer.textureCompression = 0; texture_importer.filterMode = FilterMode.Point; texture_importer.spritePixelsPerUnit = 32; assetImporter.SaveAndReimport(); } } }