Skip to content

Instantly share code, notes, and snippets.

@birdinforest
Last active August 8, 2022 20:10
Show Gist options
  • Save birdinforest/b7ab045d529de89d4455ee80cc65d03b to your computer and use it in GitHub Desktop.
Save birdinforest/b7ab045d529de89d4455ee80cc65d03b to your computer and use it in GitHub Desktop.
Apply multiple prefabs at once. Select all your prefabs and use either shortcut or Tools menu item to apply and revert. (You can change shortcuts in the script if you want, currently it uses ctrl+shft+a to apply and ctrl+shft+r to revert)
// Credit: baptisteLar from http://forum.unity3d.com/threads/little-script-apply-and-revert-several-prefab-at-once.295311/
public class ApplySelectedPrefabs : EditorWindow
{
public delegate void ApplyOrRevert(GameObject _goCurrentGo, Object _ObjPrefabParent, ReplacePrefabOptions _eReplaceOptions);
[MenuItem ("Tools/Apply all selected prefabs %#a")]
static void ApplyPrefabs()
{
SearchPrefabConnections (ApplyToSelectedPrefabs);
}
[MenuItem ("Tools/Revert all selected prefabs %#r")]
static void ResetPrefabs()
{
SearchPrefabConnections (RevertToSelectedPrefabs);
}
//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");
}
}
//Apply
static void ApplyToSelectedPrefabs(GameObject _goCurrentGo, Object _ObjPrefabParent, ReplacePrefabOptions _eReplaceOptions)
{
PrefabUtility.ReplacePrefab(_goCurrentGo, _ObjPrefabParent,_eReplaceOptions);
}
//Revert
static void RevertToSelectedPrefabs(GameObject _goCurrentGo, Object _ObjPrefabParent, ReplacePrefabOptions _eReplaceOptions)
{
PrefabUtility.ReconnectToLastPrefab(_goCurrentGo);
PrefabUtility.RevertPrefabInstance(_goCurrentGo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment