Bill's
Spills

Bilal Akil

Unity Editor Hack 2: Programatically Change Texture Import Settings for Different Platforms

This is an editor script which adds a menu button to execute the below function. It's idempotent, meaning it's safe to run again and again.

This script copies the default texture import settings to other specific platforms, in this case iPhone and Android. There is one setting that's being explicitly set on those platforms, which could not be copied from default: a specific crunch compression format. The "use crunch compression" tickbox in default didn't work on specific platforms for me, so I needed to explicitly set the compression format to ETC2_RGBA8Crunched. But most textures were being added by a collaborator who didn't have all platforms installed. Instead of asking them to install the platforms and learn how to set the compression formats, I made this editor script.

The code will:

  • Find all textures
  • Get their texture importers
  • Manage their platform specific settings
  • Save and reimport them if changed

using System;
using UnityEditor;
using UnityEngine;

public static class PrepareGame
{
    [MenuItem("Kubblammo/Advanced/Update Platform Specific Image Compression")]
    static void UpdatePlatformSpecificImageCompression()
    {
        var destFmt = TextureImporterFormat.ETC2_RGBA8Crunched;
        var numChanges = 0;

        foreach (var guid in AssetDatabase.FindAssets("t:Texture", new String[] { "Assets" }))
        {
            var path = AssetDatabase.GUIDToAssetPath(guid);
            var importer = TextureImporter.GetAtPath(path) as TextureImporter;

            // Skip things like TMPro font atlases
            if (importer == null) continue;

            var def = importer.GetDefaultPlatformTextureSettings();
            var changed = false;

            Action<TextureImporterPlatformSettings> maybeChange = (platSettings) =>
            {
                if (
                    platSettings.format != destFmt ||
                    platSettings.compressionQuality != def.compressionQuality ||
                    platSettings.maxTextureSize != def.maxTextureSize ||
                    !platSettings.overridden
                )
                {
                    platSettings.format = destFmt;
                    platSettings.compressionQuality = def.compressionQuality;
                    platSettings.maxTextureSize = def.maxTextureSize;
                    platSettings.overridden = true;

                    changed = true;
                    importer.SetPlatformTextureSettings(platSettings);
                }
            };

            maybeChange(importer.GetPlatformTextureSettings("iPhone"));
            maybeChange(importer.GetPlatformTextureSettings("Android"));

            if (changed)
            {
                importer.SaveAndReimport();
                ++numChanges;
            }
        }

        Debug.Log(String.Format("Update Platform Specific Image Compression: {0} images updated", numChanges));
    }
}

Questions? Please discuss via email, Twitter or Reddit.