Skip to content

Instantly share code, notes, and snippets.

@Pyredrid
Last active October 24, 2018 11:31
Show Gist options
  • Save Pyredrid/55063af53a3461588c2984e99100a23f to your computer and use it in GitHub Desktop.
Save Pyredrid/55063af53a3461588c2984e99100a23f to your computer and use it in GitHub Desktop.
Does the most time-consuming task to do in Unity for you: Revert and apply large selections of prefabs (Something Unity does not have by default...)
//EDITOR SCRIPT
//Put in an Editor folder
using UnityEngine;
using UnityEditor;
public static class MiscEditorTools {
//Useful to revert changes to all selected prefab instances in the scene editor
[MenuItem("CustTools/Group Prefab Revert")]
public static void RevertGroup () {
//Get every currently selected gameobject
GameObject[] selection = Selection.gameObjects;
for(int i = 0; i < selection.Length; i++) {
//Be sure its actually a prefab before we change it
if(PrefabUtility.GetPrefabParent(selection[i]) != null) {
//Reset it, aren't editor utilities nice?
PrefabUtility.ResetToPrefabState(selection[i]);
}
}
}
//Useful to apply changes to all selected prefab instances in the scene editor
[MenuItem("CustTools/Group Prefab Apply")]
public static void ApplyGroup () {
//Get every currently selected gameobject
GameObject[] selection = Selection.gameObjects;
for(int i = 0; i < selection.Length; i++) {
//Be sure its actually a prefab before we change it
if(PrefabUtility.GetPrefabParent(selection[i]) != null) {
//Editor utilities are suprisingly useless here
//Get the selection's gameobject to get the prefab root
GameObject instanceRoot = PrefabUtility.FindRootGameObjectWithSameParentPrefab(selection[i].gameObject);
//Get the parent of the prefab root
Object targetPrefab = PrefabUtility.GetPrefabParent(instanceRoot);
//Replace and update connected prefabs since we can't just simply 'apply'
PrefabUtility.ReplacePrefab(instanceRoot, targetPrefab, ReplacePrefabOptions.ConnectToPrefab);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment