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

举报
CoderZ1010 发表于 2022/09/25 05:05:05 2022/09/25
【摘要】 AssetPostprocessor允许我们在导入资源时做一些预处理或后处理,以下是官方文档给出的说明: 例如导入Texture贴图资源,我们可以通过在AssetPostprocessor的子类中添加OnPreprocessTexture函数来添加预处理逻辑,添加OnPostprocessTexture函数来实现后处理逻辑,可以理...

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

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

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

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

代码如下:


      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; } }
          }
      }
  
 

有了配置表后,在OnPreprocessTexture函数中根据配置来对TextureImporter进行设置,代码如下:


      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类型:

文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。

原文链接:coderz.blog.csdn.net/article/details/123241490

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。