//Look for connections static void SearchPrefabConnections(ApplyOrRevert _applyOrRevert) { GameObject[] tSelection = Selection.gameObjects; if (tSelection.Length > 0) { GameObject goPrefabRoot; GameObject goParent; GameObject goCur; bool bTopHierarchyFound; int iCount = 0; PrefabType prefabType; bool bCanApply; //Iterate through all the selected gameobjects foreach (GameObject go in tSelection) { prefabType = PrefabUtility.GetPrefabType(go); //Is the selected gameobject a prefab? if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance) { //Prefab Root; goPrefabRoot = ((GameObject)PrefabUtility.GetPrefabParent(go)).transform.root.gameObject; goCur = go; bTopHierarchyFound = false; bCanApply = true; //We go up in the hierarchy to apply the root of the go to the prefab while (goCur.transform.parent != null && !bTopHierarchyFound) { //Are we still in the same prefab? if (goPrefabRoot == ((GameObject)PrefabUtility.GetPrefabParent(goCur.transform.parent.gameObject)).transform.root.gameObject) { goCur = goCur.transform.parent.gameObject; } else { //The gameobject parent is another prefab, we stop here bTopHierarchyFound = true; if (goPrefabRoot != ((GameObject)PrefabUtility.GetPrefabParent(goCur))) { //Gameobject is part of another prefab bCanApply = false; } } } if (_applyOrRevert != null && bCanApply) { iCount++; _applyOrRevert(goCur, PrefabUtility.GetPrefabParent(goCur), ReplacePrefabOptions.ConnectToPrefab); } } } Debug.Log(iCount + " prefab" + (iCount > 1 ? "s" : "") + " updated"); } }
//Look for connections static void SearchPrefabConnections(ApplyOrRevert _applyOrRevert) { GameObject[] tSelection = Selection.gameObjects; if (tSelection.Length > 0) { GameObject goPrefabRoot; GameObject goParent; GameObject goCur; bool bTopHierarchyFound; int iCount = 0; PrefabType prefabType; bool bCanApply; //Iterate through all the selected gameobjects foreach (GameObject go in tSelection) { prefabType = PrefabUtility.GetPrefabType(go); //Is the selected gameobject a prefab? if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance) { //Prefab Root; goPrefabRoot = ((GameObject)PrefabUtility.GetPrefabParent(go)).transform.root.gameObject; goCur = go; bTopHierarchyFound = false; bCanApply = true; //We go up in the hierarchy to apply the root of the go to the prefab while (goCur.transform.parent != null && !bTopHierarchyFound) { //Are we still in the same prefab? if (PrefabUtility.GetPrefabParent(goCur.transform.parent.gameObject) != null && (goPrefabRoot == ((GameObject)PrefabUtility.GetPrefabParent(goCur.transform.parent.gameObject)).transform.root.gameObject)) { goCur = goCur.transform.parent.gameObject; } else { //The gameobject parent is another prefab, we stop here bTopHierarchyFound = true; if (goPrefabRoot != ((GameObject)PrefabUtility.GetPrefabParent(goCur))) { //Gameobject is part of another prefab bCanApply = false; } } } if (_applyOrRevert != null && bCanApply) { iCount++; _applyOrRevert(goCur, PrefabUtility.GetPrefabParent(goCur), ReplacePrefabOptions.ConnectToPrefab); } } } Debug.Log(iCount + " prefab" + (iCount > 1 ? "s" : "") + " updated"); } }
// Look for connections public static string SearchPrefabConnections(ApplyOrRevert _applyOrRevert, PrefabAction action) { GameObject[] objs = Selection.gameObjects; if (action == PrefabAction.Revert) { // Display warning dialog box if revert is chosen if (!EditorUtility.DisplayDialog("Revert multiple prefabs?", "You are attempting to revert the properties of multiple prefabs. All current modifications to these prefabs will be lost. Are you sure?", "Yes", "No")) { return("Revert cancelled."); } } if (!SelectedItemsValid(objs)) { return("Error: Cannot create prefabs that already exist"); } if (objs.Length > 1) { GameObject goPrefabRoot; GameObject goCur; bool isTopHierarchyFound; int prefabCount = 0; PrefabType prefabType; bool canApply; // Iterate through all the selected gameobjects foreach (GameObject go in objs) { prefabType = PrefabUtility.GetPrefabType(go); // Is the selected gameobject a prefab? if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance) { // Prefab Root goPrefabRoot = ((GameObject)PrefabUtility.GetPrefabParent(go)).transform.root.gameObject; goCur = go; isTopHierarchyFound = false; canApply = true; // We go up in the hierarchy to apply the root of the go to the prefab while (goCur.transform.parent != null && !isTopHierarchyFound) { // Are we still in the same prefab? if (PrefabUtility.GetPrefabParent(goCur.transform.parent.gameObject) != null && (goPrefabRoot == ((GameObject)PrefabUtility.GetPrefabParent(goCur.transform.parent.gameObject)).transform.root.gameObject)) { goCur = goCur.transform.parent.gameObject; } else { // The GameObject parent is another prefab, we stop here isTopHierarchyFound = true; if (goPrefabRoot != ((GameObject)PrefabUtility.GetPrefabParent(goCur))) { // GameObject is part of another prefab canApply = false; } } } if (_applyOrRevert != null && canApply) { prefabCount++; _applyOrRevert(goCur, PrefabUtility.GetPrefabParent(goCur), ReplacePrefabOptions.ConnectToPrefab); } } } if (action == PrefabAction.Apply) { return(prefabCount + " prefabs updated."); } else { return(prefabCount + " prefabs reverted."); } } else { return("Error: Methods will only work with multiple items selected."); } }