Unity 6.3
0 онлайн 34 гостей 3 в системе
Вход

PrefabUtility.ApplyRemovedGameObject

Declaration

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

Параметры

Параметр Описание
gameObjectInInstance GameObject в экземпляре Префаб, содержащем удаленный GameObject.
assetGameObject GameObject в ассете Префаб, соответствующем удаленному GameObject на экземпляре.
действия Режим взаимодействия для этого действия.

Описание

Удаляет GameObject из исходного ассета Префаб.

Когда экземпляр GameObject удаляется из экземпляра Префаб, это изменение является типом Переопределение экземпляра. Применение изменения (удаления GameObject) к префабу означает, что GameObject удаляется из самого ассета Prefab вместе со своими компонентами и дочерними GameObject и перестаёт быть переопределением в экземпляре префаба.

При применении удаленного GameObject к ассету Префаб, вы должны указать путь ассета в качестве параметра. Это потому, что в некоторых ситуациях существует несколько возможных целей для применения изменения. Например, если GameObject был удален из GameObject, который является частью вложенные Префаб например, у вас может быть выбор применить изменение к внутреннему вложенному ассету Префаб или к внешнему корню ассета Префаб. Поэтому, указывая путь ассета, вы ясно даете понять Unity, к какому ассету Префаб должно быть применено изменение. Это зеркало Применить в редакторе, как это описано в разделе Применить переопределения к ассету префаб документация.

Более подробную информацию см. Переопределение экземпляров префаб.

Дополнительные ресурсы: PrefabUtility.ApplyAddedComponent, PrefabUtility.ApplyAddedGameObject, PrefabUtility.ApplyObjectOverride, PrefabUtility.ApplyPrefabInstance, PrefabUtility.ApplyPropertyOverride, PrefabUtility.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"); } }