Skip to content

Instantly share code, notes, and snippets.

@madsbangh
Created September 29, 2017 13:04
using UnityEditor;
using UnityEngine;
public class ReplacerWizard : ScriptableWizard
{
[SerializeField] private GameObject prefab = null;
[MenuItem("Tools/Replacer")]
static void CreateWizard()
{
DisplayWizard<ReplacerWizard>("Replace GameObjects", "Replace");
}
void OnWizardCreate()
{
Undo.SetCurrentGroupName("Replace");
foreach (var oldObject in Selection.GetFiltered<GameObject>(SelectionMode.ExcludePrefab))
{
GameObject newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
newObject.transform.parent = oldObject.transform.parent;
newObject.transform.localPosition = oldObject.transform.localPosition;
newObject.transform.localRotation = oldObject.transform.localRotation;
newObject.transform.localScale = oldObject.transform.localScale;
Undo.RegisterCreatedObjectUndo(newObject, "Instantiate " + newObject.name);
Undo.DestroyObjectImmediate(oldObject);
}
}
void OnWizardUpdate()
{
helpString = "Choose a prefab with which to replace all selected GameObjects";
isValid = prefab && !prefab.scene.IsValid();
errorString = isValid ? "" : "Select a prefab from Assets";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment