Version: 2021.3

AssetPostprocessor.OnPostprocessPrefab(GameObject root)

切换到手册

描述

Gets a notification when a Prefab completes importing.

To use this function, add it to a subclass. It lets you modify the imported GameObject. GameObjects only exist during the import and Unity destroys them immediately after import.

This function is called before the imported Prefab is created in the Library folder and before it is written to disk. Therefore, you have full control over the generated GameObjects and Components.

Any references to GameObjects become invalid after Unity completes the import. As such, you cannot create a new Prefab in a different file from OnPostprocessPrefab that references meshes in the imported Prefab file.

To add new Asset objects to the Prefab, call AssetPostprocessor.context.AddObjectToAsset()

The postprocessor can set or modify hideflags on objects in the Prefab. Added asset objects always get the DontSaveInEditor and NotEditable flags added. Added GameObjects and Components always get the DontSaveInEditor flag added. The DontSaveInEditor flag is always set to avoid the object from being saved back into the Prefab source asset, because this duplicates the generated objects every time the Prefab is saved.

root is the root GameObject of the imported Prefab.

using UnityEngine;
using UnityEditor;

// Adds a mesh collider to each game object that contains collider in its name public class Example : AssetPostprocessor { void OnPostprocessPrefab(GameObject g) { Apply(g.transform); }

void Apply(Transform t) { if (t.name.ToLower().Contains("collider")) t.gameObject.AddComponent<MeshCollider>();

// Recurse foreach (Transform child in t) Apply(child); } }