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) { var danglingObjects = new List <Object>(); PrefabUtility.CollectAddedObjects(t.correspondingSourceObject, t.prefabHierarchy, danglingObjects); foreach (var component in danglingObjects) { Undo.RegisterCreatedObjectUndoToFrontOfUndoQueue(component, actionName); } } EditorUtility.ForceRebuildInspectors(); return(true); }