Version: Unity 6.6 Alpha (6000.6)
LanguageEnglish
  • C#

AssetPostprocessor.OnPreprocessAsset()

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Add this function to a subclass to get a notification just before any Asset is imported.

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 GetVersion does not trigger a reimport of affected assets. To register dependencies explicitly, use AssetImportContext.DependsOnCustomDependency or AssetImportContext.DependsOnArtifact through the context property.

Note: If your subclass also implements another callback such as OnPreprocessTexture, the dependency is registered by that callback and GetVersion works as expected.

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