Skip to content

Instantly share code, notes, and snippets.

@marcelschmidtdev
Created October 23, 2017 17:08
Show Gist options
  • Save marcelschmidtdev/e7cd29fe43785bbd66f604a17381ad0d to your computer and use it in GitHub Desktop.
Save marcelschmidtdev/e7cd29fe43785bbd66f604a17381ad0d to your computer and use it in GitHub Desktop.
Helper menu to really break Unity prefab instances (removes the Inspector prefab buttons)
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
public class GameObjectTools
{
private const string TempPrefabAssetPath = "Assets/tmp.prefab";
[MenuItem ("GameObject/Really Break Prefab Instance", true)]
public static bool ValidateBreakPrefabInstance ()
{
GameObject selected = Selection.activeGameObject;
if (selected == null)
{
return false;
}
PrefabType prefabType = PrefabUtility.GetPrefabType (selected);
return prefabType != PrefabType.None && prefabType != PrefabType.Prefab && prefabType != PrefabType.ModelPrefab;
}
[MenuItem ("GameObject/Really Break Prefab Instance")]
public static void BreakPrefabInstance ()
{
GameObject selected = Selection.activeGameObject;
Undo.RegisterFullObjectHierarchyUndo (selected, "Really Break Prefab Instance");
Selection.activeGameObject = null;
Object prefab = PrefabUtility.CreateEmptyPrefab (TempPrefabAssetPath);
PrefabUtility.ReplacePrefab (selected, prefab, ReplacePrefabOptions.ConnectToPrefab);
PrefabUtility.DisconnectPrefabInstance (selected);
AssetDatabase.DeleteAsset (TempPrefabAssetPath);
AssetDatabase.Refresh ();
Selection.activeGameObject = selected;
EditorSceneManager.MarkSceneDirty (EditorSceneManager.GetActiveScene ());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment