前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity【AssetPostprocessor】- 资源导入处理:TexturePreprocessor

Unity【AssetPostprocessor】- 资源导入处理:TexturePreprocessor

作者头像
CoderZ
发布2022-08-29 16:41:02
8480
发布2022-08-29 16:41:02
举报
文章被收录于专栏:用户10004205的专栏

AssetPostprocessor允许我们在导入资源时做一些预处理或后处理,以下是官方文档给出的说明:

例如导入Texture贴图资源,我们可以通过在AssetPostprocessor的子类中添加OnPreprocessTexture函数来添加预处理逻辑,添加OnPostprocessTexture函数来实现后处理逻辑,可以理解为导入贴图资源前和完成时的事件、回调。

本文以OnPreprocessTexture为例,首先来看Texture Importer的主要属性:

我们通过继承ScriptableObject类来实现一个以上属性的配置表:

代码如下:

代码语言:javascript
复制
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; } }
    }
}
代码语言:javascript
复制
有了配置表后,在OnPreprocessTexture函数中根据配置来对TextureImporter进行设置,代码如下:
代码语言:javascript
复制
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;
        }
    }
}

例如我们在搭建UI时,可以将Texture Type设为Sprite类型,那么在将UI切图导入到Unity中时,将自动将其设为Sprite类型:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-03-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 当代野生程序猿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档