Version: 2020.2

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 final Prefab is created 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. Asset objects always get the DontSaveInEditor and NotEditable flags added. GameObejcts 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); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961