Skip to content

Instantly share code, notes, and snippets.

@flycarl
Created February 24, 2017 08:06
Show Gist options
  • Save flycarl/b16acfc38ee968df22f085bbf3aef305 to your computer and use it in GitHub Desktop.
Save flycarl/b16acfc38ee968df22f085bbf3aef305 to your computer and use it in GitHub Desktop.
Unity Editor Script, put a prefab in editor window, select a transform in Hierarchy, then click Replace button, the tranform will replace with the prefab.
using UnityEngine;
using UnityEditor;
public class ReplacePrefab : EditorWindow {
[MenuItem("GameEditor/ReplacePrefab")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(ReplacePrefab));
}
static public GameObject source;
public void OnGUI()
{
if (GUILayout.Button("Replace the selected GameObjects with your prefab"))
{
ReplaceSelected();
}
EditorGUILayout.BeginHorizontal();
source = EditorGUILayout.ObjectField("Prefab", source, typeof(GameObject), true) as GameObject;
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("Rename delete (clone)"))
{
Rename();
}
}
private static void ReplaceSelected()
{
GameObject[] go = Selection.gameObjects;
int go_count = 0;
foreach (GameObject g in go)
{
go_count++;
Transform trans = g.transform;
Transform parent = trans.parent;
GameObject instance = Instantiate(source) as GameObject;
instance.transform.parent = parent;
instance.transform.localPosition = trans.localPosition;
instance.transform.localRotation = trans.localRotation;
instance.transform.localScale = trans.localScale;
GameObject.DestroyImmediate(g);
string name = instance.name;
instance.name = name.Substring(0, name.Length - 7);
}
Debug.Log(string.Format("Replaced {0} GameObjects", go_count));
}
private static void Rename()
{
GameObject[] go = Selection.gameObjects;
int go_count = 0, components_count = 0, missing_count = 0;
foreach (GameObject g in go)
{
go_count++;
string name = g.name;
g.name = name.Substring(0, name.Length - 7);
}
Debug.Log(string.Format("Replaced {0} GameObjects", go_count));
}
}
@dimmduh
Copy link

dimmduh commented May 6, 2022

Thx, but Undo (Ctrl + z) does not work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment