AssetPostprocessorを使用すると、アセットをインポートするときに前処理または後処理を行うことができます。公式ドキュメントに記載されている手順は次のとおりです。
たとえば、テクスチャテクスチャリソースをインポートするには、AssetPostprocessorのサブクラスにOnPreprocessTexture関数を追加して前処理ロジックを追加し、OnPostprocessTexture関数を追加して後処理ロジックを実装します。これは、前後のイベントとコールバックとして理解できます。テクスチャリソースのインポート。
この記事では、例としてOnPreprocessTextureを取り上げます。まず、TextureImporterの主なプロパティを確認します。
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を構築するときに、テクスチャタイプをスプライトタイプに設定し、UIスライスをUnityにインポートすると、自動的にスプライトタイプに設定されます。
パブリックアカウント「ContemporaryWildProgrammer」へようこそ