internal static void RevertPrefabInstanceWithUndo(GameObject target) { string text = "Revert Prefab Instance"; PrefabType prefabType = PrefabUtility.GetPrefabType(target); bool flag = prefabType == PrefabType.DisconnectedModelPrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance; GameObject gameObject; if (flag) { gameObject = PrefabUtility.FindRootGameObjectWithSameParentPrefab(target); } else { gameObject = PrefabUtility.FindValidUploadPrefabInstanceRoot(target); } List <UnityEngine.Object> hierarchy = new List <UnityEngine.Object>(); PrefabUtility.GetObjectListFromHierarchy(hierarchy, gameObject); Undo.RegisterFullObjectHierarchyUndo(gameObject, text); if (flag) { PrefabUtility.ReconnectToLastPrefab(gameObject); Undo.RegisterCreatedObjectUndo(PrefabUtility.GetPrefabObject(gameObject), text); } PrefabUtility.RevertPrefabInstance(gameObject); List <UnityEngine.Object> list = new List <UnityEngine.Object>(); PrefabUtility.GetObjectListFromHierarchy(list, PrefabUtility.FindPrefabRoot(gameObject)); PrefabUtility.RegisterNewObjects(list, hierarchy, text); }
internal static void ReplacePrefabWithUndo(GameObject target) { string text = "Apply instance to prefab"; UnityEngine.Object prefabParent = PrefabUtility.GetPrefabParent(target); GameObject gameObject = PrefabUtility.FindValidUploadPrefabInstanceRoot(target); Undo.RegisterFullObjectHierarchyUndo(prefabParent, text); Undo.RegisterFullObjectHierarchyUndo(gameObject, text); Undo.RegisterCreatedObjectUndo(gameObject, text); List <UnityEngine.Object> hierarchy = new List <UnityEngine.Object>(); PrefabUtility.GetObjectListFromHierarchy(hierarchy, prefabParent as GameObject); PrefabUtility.ReplacePrefab(gameObject, prefabParent, ReplacePrefabOptions.ConnectToPrefab); List <UnityEngine.Object> list = new List <UnityEngine.Object>(); PrefabUtility.GetObjectListFromHierarchy(list, prefabParent as GameObject); PrefabUtility.RegisterNewObjects(list, hierarchy, text); }
bool ApplyAll() { // Collect Prefab Asset paths and also check if there's more than one of the same. HashSet <string> prefabAssetPaths = new HashSet <string>(); bool multipleOfSame = false; for (int i = 0; i < m_SelectedGameObjects.Length; i++) { string prefabAssetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(m_SelectedGameObjects[i]); if (prefabAssetPaths.Contains(prefabAssetPath)) { multipleOfSame = true; } else { prefabAssetPaths.Add(prefabAssetPath); } } // If more than one instance of the same Prefab Asset, show dialog to user. if (multipleOfSame && !EditorUtility.DisplayDialog( L10n.Tr("Multiple instances of same Prefab Asset"), L10n.Tr("Multiple instances of the same Prefab Asset were detected. Potentially conflicting overrides will be applied sequentially and will overwrite each other."), L10n.Tr("OK"), L10n.Tr("Cancel"))) { return(false); } // Make sure assets are checked out in version control. if (!PrefabUtility.PromptAndCheckoutPrefabIfNeeded(prefabAssetPaths.ToArray(), PrefabUtility.SaveVerb.Apply)) { return(false); } var undoStructs = new List <ApplyAllUndo>(); var actionName = "ApplyAll"; for (var i = 0; i < m_SelectedGameObjects.Length; i++) { var us = new ApplyAllUndo(); us.correspondingSourceObject = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(m_SelectedGameObjects[i]); Undo.RegisterFullObjectHierarchyUndo(us.correspondingSourceObject, actionName); // handles changes to existing objects and object what will be deleted but not objects that are created GameObject prefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(m_SelectedGameObjects[i]); Undo.RegisterFullObjectHierarchyUndo(prefabInstanceRoot, actionName); us.prefabHierarchy = new HashSet <int>(); PrefabUtility.GetObjectListFromHierarchy(us.prefabHierarchy, us.correspondingSourceObject); undoStructs.Add(us); } // Apply sequentially. AssetDatabase.StartAssetEditing(); try { foreach (var t in m_SelectedGameObjects) { PrefabUtility.ApplyPrefabInstance(t, InteractionMode.UserAction); } } finally { AssetDatabase.StopAssetEditing(); } foreach (var t in undoStructs) { PrefabUtility.RegisterNewObjects(t.correspondingSourceObject, t.prefabHierarchy, actionName); } EditorUtility.ForceRebuildInspectors(); return(true); }