Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


OnPreprocessAsset()

Add this function to a subclass to get a notification just before any Asset is imported.
Read time 1 minuteLast updated 12 days ago

Definition

public void OnPreprocessAsset()

Remarks

This lets you control the import settings through code. Unlike other `OnPreprocess` callbacks such as OnPreprocessTexture or OnPreprocessModel, `OnPreprocessAsset` does not automatically register a dependency on the postprocessor. This means that if your subclass only implements `OnPreprocessAsset`, changing the value returned by AssetPostprocessor.GetVersion does not trigger a reimport of affected assets. To register dependencies explicitly, use AssetImportContext.DependsOnCustomDependency or AssetImportContext.DependsOnArtifact through the AssetPostprocessor.context property. Note: If your subclass also implements another callback such as OnPreprocessTexture, the dependency is registered by that callback and AssetPostprocessor.GetVersion works as expected.

Examples

using UnityEditor;class PresetEnforcer : AssetPostprocessor{ void OnPreprocessAsset() { if (assetImporter.importSettingsMissing) { ModelImporter modelImporter = assetImporter as ModelImporter; if (modelImporter != null) { if (!assetPath.Contains("@")) modelImporter.importAnimation = false; modelImporter.materialImportMode = ModelImporterMaterialImportMode.None; } } }}
using UnityEditor;using UnityEngine;//The following example registers an explicit custom dependency so that changing the dependency hash triggers a reimport of affected assets.class PresetByFolderPostprocessor : AssetPostprocessor{ static readonly uint k_Version = 1; public override uint GetVersion() { return k_Version; } [InitializeOnLoadMethod] static void RegisterDependency() { AssetDatabase.RegisterCustomDependency( "PresetByFolderPostprocessor", Hash128.Compute(k_Version.ToString())); } void OnPreprocessAsset() { context.DependsOnCustomDependency("PresetByFolderPostprocessor"); if (assetImporter.importSettingsMissing) { // Apply default import settings based on folder location. } }}