【Unity】テクスチャのフォーマットを変換する後処理

DEVELOP, Unity

テクスチャの圧縮設定などを一括で行う簡単なスクリプトです。テクスチャインポート時に OnPreprocessTexture が呼ばれるのでこれを利用して設定を上書いています。

using System;
using System.Collections.Generic;
using UnityEditor;
namespace Editor
{
    /// <summary>
    /// String 拡張。
    /// </summary>
    public static class StringExtensions
    {
        // 大文字小文字を無視して比較します。
        public static bool Contains(this string source, string toCheck, StringComparison comp)
        {
            return source.IndexOf(toCheck, comp) >= 0;
        }
    }
    /// <summary>
    /// asset post process all.
    /// </summary>
    public sealed class GameTextureAssetPostprocessor : AssetPostprocessor
    {
        string[] TARGET_DIRECTORY_NAME =
        {
            "Assets/Resources",
        };
        public enum Category
        {
            Character,      // キャラクター
            NUM,
        };
        private Dictionary<Category, string> m_categoryDict = new Dictionary<Category, string>()
        {
            { Category.CharacterCard,  "Character"},
        };
        /// <summary>
        /// 設定を上書きすべきカテゴリー?
        /// </summary>
        bool isOverrideSettingCategory(Category _category, TextureImporter textureImporter)
        {
            string result = string.Empty;
            if(m_categoryDict.TryGetValue(_category, out result))
            {
                if (textureImporter.assetPath.Contains(result, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// テクスチャ更新。
        /// </summary>
        void OnPreprocessTexture()
        {
            /// ディレクトリ更新チェック。
            if (!IsUpdateDirectory(assetImporter, TARGET_DIRECTORY_NAME))
            {
                return;
            }
            TextureImporter textureImporter = assetImporter as TextureImporter;
            bool                    isOverridSettings  = false;
            bool                    useMipMap          = false;
            int                     maxTextureSize      = 512;
            TextureImporterFormat   textureFormatAndroid = TextureImporterFormat.ETC2_RGBA8;
            TextureImporterFormat   textureFormatiOS = TextureImporterFormat.ASTC_RGBA_6x6;
            if (isOverrideSettingCategory(Category.CharacterCard, textureImporter))
            {
                isOverridSettings = true;
                useMipMap = false;
                maxTextureSize = 1024;
                textureFormatAndroid = TextureImporterFormat.ETC_RGB4;
                textureFormatiOS = TextureImporterFormat.ASTC_RGBA_6x6;
            }
            if(isOverridSettings)
            {
                bool isUpdateFile = false;
                // 共通設定。
                if(textureImporter.mipmapEnabled != useMipMap)
                {
                    textureImporter.mipmapEnabled = useMipMap;
                    isUpdateFile = true;
                }
                {
                    // Settings Android.
                    TextureImporterPlatformSettings android_settings    = textureImporter.GetPlatformTextureSettings("Android");
                    if(android_settings.format != textureFormatAndroid || android_settings.maxTextureSize != maxTextureSize)
                    {
                        android_settings.overridden                         = isOverridSettings;
                        android_settings.name                               = "Android";
                        android_settings.maxTextureSize                     = maxTextureSize;
                        android_settings.format                             = textureFormatAndroid;
                        textureImporter.SetPlatformTextureSettings(android_settings);
                        isUpdateFile = true;
                    }
                    // Settings iOS.
                    TextureImporterPlatformSettings ios_settings        = textureImporter.GetPlatformTextureSettings("iPhone");
                    if(ios_settings.format != textureFormatiOS || ios_settings.maxTextureSize != maxTextureSize)
                    {
                        ios_settings.overridden                             = isOverridSettings;
                        ios_settings.name                                   = "iPhone";
                        ios_settings.maxTextureSize                         = maxTextureSize;
                        ios_settings.format                                 = textureFormatiOS;
                        textureImporter.SetPlatformTextureSettings(ios_settings);
                        isUpdateFile = true;
                    }
                    if(isUpdateFile)
                    {
                        EditorUtility.SetDirty (textureImporter);
                        textureImporter.SaveAndReimport();
                        AssetDatabase.SaveAssets ();
                        AssetDatabase.Refresh ();
                    }
                }
            }
        }
    }
}

Posted by kazupon