Unity [AssetPostprocessor] - resource import processing: TexturePreprocessor

AssetPostprocessor allows us to do some preprocessing or postprocessing when importing assets. The following are the instructions given by the official documentation:

For example, to import Texture texture resources, we can add the preprocessing logic by adding the OnPreprocessTexture function in the subclass of AssetPostprocessor, and add the OnPostprocessTexture function to implement the post-processing logic, which can be understood as events and callbacks before and when importing texture resources.

This article takes OnPreprocessTexture as an example, first look at the main properties of Texture Importer:

We implement a configuration table with more than one property by inheriting the ScriptableObject class:

code show as below:

using System.IO;
using UnityEditor;
using UnityEngine;

namespace SK.Framework
{
    [CreateAssetMenu]
    public class TexturePreprocessorConfig : ScriptableObject
    {
        [SerializeField] private bool isEnabled = false;

        [SerializeField] private TextureImporterType textureType = TextureImporterType.Default;
        [SerializeField] private TextureImporterShape textureShape = TextureImporterShape.Texture2D;
        [SerializeField] private bool sRGBTexture = true;
        [SerializeField] private TextureImporterAlphaSource alphaSource = TextureImporterAlphaSource.FromInput;
        [SerializeField] private bool alphaIsTransparency;
        [SerializeField] private bool ignorePNGFileGamma;

        [Header("Advanced")]
        [SerializeField] private TextureImporterNPOTScale nonPowerOf2 = TextureImporterNPOTScale.ToNearest;
        [SerializeField] private bool readWriteEnabled;
        [SerializeField] private bool streamingMipmaps;
        [SerializeField] private bool vitrualTextureOnly;
        [SerializeField] private bool generateMipMaps = true;
        [SerializeField] private bool borderMipMaps;
        [SerializeField] private TextureImporterMipFilter mipmapFilter = TextureImporterMipFilter.BoxFilter;
        [SerializeField] private bool mipMapsPreserveCoverage;
        [SerializeField] private bool fadeoutMipMaps;

        [SerializeField] private TextureWrapMode wrapMode = TextureWrapMode.Repeat;
        [SerializeField] private FilterMode filterMode = FilterMode.Bilinear;
        [SerializeField, Range(0, 16)] private int anisoLevel = 1;

        [SerializeField] private int maxSize = 2048;
        [SerializeField] private TextureImporterFormat format = TextureImporterFormat.Automatic;
        [SerializeField] private TextureImporterCompression compression = TextureImporterCompression.Compressed;
        [SerializeField] private bool useCrunchCompression;

        private static TexturePreprocessorConfig config;
        private static TexturePreprocessorConfig Config
        {
            get
            {
                if (config == null)
                {
                    var path = "Assets/Profile/Texture Postprocessor Config.asset";
                    config = AssetDatabase.LoadAssetAtPath<TexturePreprocessorConfig>(path);
                    if (config == null)
                    {
                        config = CreateInstance<TexturePreprocessorConfig>();
                        var directory = Application.dataPath + "/Profile";
                        if (!Directory.Exists(directory))
                        {
                            Directory.CreateDirectory(Application.dataPath + "/Profile");
                        }
                        AssetDatabase.CreateAsset(config, path);
                        AssetDatabase.Refresh();
                    }
                }
                return config;
            }
        }

        public static bool IsEnabled { get { return Config.isEnabled; } }
        public static TextureImporterType TextureType { get { return Config.textureType; } }
        public static TextureImporterShape TextureShape { get { return Config.textureShape; } }
        public static bool SRGBTexture { get { return Config.sRGBTexture; } }
        public static TextureImporterAlphaSource AlphaSource { get { return Config.alphaSource; } }
        public static bool AlphaIsTransparency { get { return Config.alphaIsTransparency ; } }
        public static bool IgnorePNGFileGamma { get { return Config.ignorePNGFileGamma; } }

        public static TextureImporterNPOTScale NonPowerOf2 { get { return Config.nonPowerOf2; } }
        public static bool ReadWriteEnabled { get { return Config.readWriteEnabled; } }
        public static bool StreamingMipmaps { get { return Config.streamingMipmaps; } }
        public static bool VitrualTextureOnly { get { return Config.vitrualTextureOnly; } }
        public static bool GenerateMipMaps { get { return Config.generateMipMaps; } }
        public static bool BorderMipMaps { get { return Config.borderMipMaps; } }
        public static TextureImporterMipFilter MipmapFilter { get { return Config.mipmapFilter; } }
        public static bool MipMapsPreserveCoverage { get { return Config.mipMapsPreserveCoverage; } }
        public static bool FadeoutMipMaps { get { return Config.fadeoutMipMaps; } }

        public static TextureWrapMode WrapMode { get { return Config.wrapMode; } }
        public static FilterMode FilterMode { get { return Config.filterMode; } }
        public static int AnisoLevel { get { return Config.anisoLevel; } }

        public static int MaxSize { get { return Config.maxSize; } }
        public static TextureImporterFormat Format { get { return Config.format; } }
        public static TextureImporterCompression Compression { get { return Config.compression; } }
        public static bool UseCrunchCompression { get { return Config.useCrunchCompression; } }
    }
}

After having the configuration table, set the TextureImporter according to the configuration in the OnPreprocessTexture function. The code is as follows:

using UnityEngine;
using UnityEditor;

namespace SK.Framework
{
    public class TexturePreprocessor : AssetPostprocessor
    {
        public void OnPreprocessTexture()
        {
            if (!TexturePreprocessorConfig.IsEnabled) return;
            Debug.Log($"OnPreprocessTexture {assetPath}");
            TextureImporter importer = assetImporter as TextureImporter;
            if (importer == null) return;

            importer.textureShape = TexturePreprocessorConfig.TextureShape;
            importer.sRGBTexture = TexturePreprocessorConfig.SRGBTexture;
            importer.alphaSource = TexturePreprocessorConfig.AlphaSource;
            importer.alphaIsTransparency = TexturePreprocessorConfig.AlphaIsTransparency;
            importer.ignorePngGamma = TexturePreprocessorConfig.IgnorePNGFileGamma;
            importer.npotScale = TexturePreprocessorConfig.NonPowerOf2;
            importer.isReadable = TexturePreprocessorConfig.ReadWriteEnabled;
            importer.streamingMipmaps = TexturePreprocessorConfig.StreamingMipmaps;
            importer.vtOnly = TexturePreprocessorConfig.VitrualTextureOnly;
            importer.mipmapEnabled = TexturePreprocessorConfig.GenerateMipMaps;
            importer.borderMipmap = TexturePreprocessorConfig.BorderMipMaps;
            importer.mipmapFilter = TexturePreprocessorConfig.MipmapFilter;
            importer.mipMapsPreserveCoverage = TexturePreprocessorConfig.MipMapsPreserveCoverage;
            importer.fadeout = TexturePreprocessorConfig.FadeoutMipMaps;
            importer.wrapMode = TexturePreprocessorConfig.WrapMode;
            importer.filterMode = TexturePreprocessorConfig.FilterMode;
            importer.anisoLevel = TexturePreprocessorConfig.AnisoLevel;
            importer.maxTextureSize = TexturePreprocessorConfig.MaxSize;
            importer.textureFormat = TexturePreprocessorConfig.Format;
            importer.textureCompression = TexturePreprocessorConfig.Compression;
            importer.crunchedCompression = TexturePreprocessorConfig.UseCrunchCompression;
            importer.textureType = TexturePreprocessorConfig.TextureType;
        }
    }
}

For example, when we build the UI, we can set the Texture Type to the Sprite type, then when the UI slice is imported into Unity, it will be automatically set to the Sprite type:

  Welcome to the public account "Contemporary Wild Programmer"

Guess you like

Origin blog.csdn.net/qq_42139931/article/details/123241490