版本:Unity 6 (6000.0)
语言英语
  • C#

PrefabUtility.ApplyRemovedGameObject

提出变更建议

成功!

感谢您帮助我们改善 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们确实会阅读用户提出的每条变更建议,并在适用的情况下进行更新。

关闭

提交失败

出于某种原因,您的变更建议无法提交。请在几分钟后<<a>重试</a>。感谢您抽出时间帮助我们改善 Unity 文档的质量。

关闭

取消

声明

public static void ApplyRemovedGameObject(GameObject gameObjectInInstance, GameObject assetGameObject, InteractionMode action);

参数

gameObjectInInstance 包含已移除 GameObject 的预制实例中的 GameObject。
assetGameObject 预制资源中对应的实例中已移除 GameObject 的 GameObject。
action 此操作的交互模式。

描述

从源预制资源中移除 GameObject。

当 GameObject 从预制实例中移除后,该修改是 实例覆盖 的一种类型。将更改(移除 GameObject)应用于预制的方式表示从预制资源本身移除 GameObject 及其组件和 GameObject 子项,并且不再是预制实例上的覆盖项。

将移除的 GameObject 应用到预制资源时,必须将资源路径作为参数提供。这是因为有些情况下,有多个可应用于更改的可能目标。例如,如果 GameObject 是从 嵌套预制 实例的一部分的 GameObject 移除的,则可以选择将更改应用于内部嵌套预制资源或外部根预制资源。因此,通过指定资源路径,向 Unity 明确说明必须将更改应用于哪个预制资源。反映 此处用户手册 中描述的编辑器功能。

可在用户手册中了解更多关于 应用目标的选择 的信息。

其他资源:PrefabUtility.ApplyAddedComponentPrefabUtility.ApplyAddedGameObjectPrefabUtility.ApplyObjectOverridePrefabUtility.ApplyPrefabInstancePrefabUtility.ApplyPropertyOverridePrefabUtility.ApplyRemovedComponent

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;

// Creates new menu items under 'Examples' in the main menu. public class ApplyRemovedGameObjectExample { [MenuItem("Examples/ApplyRemovedGameObject Example 1")] static void CreatePrefabAndApplyChanges() { //Ensure the existence of a Prefabs folder inside the Assets folder if (!Directory.Exists("Assets/Prefabs")) AssetDatabase.CreateFolder("Assets", "Prefabs");

//Setup hierarchy with root and one child GameObject rootGameObject = new GameObject("Root"); GameObject child = new GameObject("Child"); child.transform.parent = rootGameObject.transform;

//Create prefab based on the GameObject hierarchy we just created GameObject prefabAsset = PrefabUtility.SaveAsPrefabAssetAndConnect(rootGameObject, "Assets/Prefabs/" + rootGameObject.name + ".prefab", InteractionMode.AutomatedAction);

//Get the corresponding object matching the Child GameObject that was destroyed GameObject correspondingChildGameObject = prefabAsset.transform.GetChild(0).gameObject;

//Destroy child GameObject so we can apply the override to the Prefab Object.DestroyImmediate(child);

//Use the variables from above to apply the removed GameObject override to the Prefab asset PrefabUtility.ApplyRemovedGameObject(rootGameObject, correspondingChildGameObject, InteractionMode.AutomatedAction);

if (prefabAsset.transform.childCount == 0) Debug.Log("'Child' GameObject was removed and the override was applied to the Prefab successfully."); else Debug.Log("The override was not applied successfully"); }

[MenuItem("Examples/ApplyRemovedGameObject Example 2")] static void CreatePrefabAndApplyChangesWithGetRemovedGameObjects() { //Ensure the existence of a Prefabs folder inside the Assets folder if (!Directory.Exists("Assets/Prefabs")) AssetDatabase.CreateFolder("Assets", "Prefabs");

//Setup hierarchy with root and one child GameObject rootGameObject = new GameObject("Root"); GameObject child = new GameObject("Child"); child.transform.parent = rootGameObject.transform;

//Create prefab based on the GameObject hierarchy we just created GameObject prefabAsset = PrefabUtility.SaveAsPrefabAssetAndConnect(rootGameObject, "Assets/Prefabs/" + rootGameObject.name + ".prefab", InteractionMode.AutomatedAction);

//Destroy child GameObject so we can apply the override to the Prefab Object.DestroyImmediate(child);

//Get the override and the information to apply the changes to the Prefab asset List<RemovedGameObject> removedGameObjects = PrefabUtility.GetRemovedGameObjects(rootGameObject); GameObject assetGameObject = removedGameObjects[0].assetGameObject; GameObject parentOfRemovedGameObjectInInstance = removedGameObjects[0].parentOfRemovedGameObjectInInstance;

//Use the variables from above to apply the removed GameObject override to the Prefab PrefabUtility.ApplyRemovedGameObject(parentOfRemovedGameObjectInInstance, assetGameObject, InteractionMode.AutomatedAction);

if (prefabAsset.transform.childCount == 0) Debug.Log("'Child' GameObject was removed and the override was applied to the Prefab successfully."); else Debug.Log("The override was not applied successfully"); } }