- Home /
Force Rename of GameObject in Hierarchy
Hi all,
I have an editor script that clones an existing GameObject then selects it in the hierarchy. I want to rename the GameObject immediately after selection, mimicing the F2 rename feature.
Any way to do this in code?
Thanks.
If what I'm asking isn't possible, is it at least possible to set Focus to the Hierarchy?
Answer by Oribow · Apr 10, 2016 at 10:49 PM
The script posted in the old answer no longer works in newer unity versions. Use this instead:
using UnityEngine;
using UnityEditor;
using System.Reflection;
public class ObjClone : EditorWindow
{
// Add menu named "My Window" to the Window menu
[MenuItem("Window/ObjClone")]
static void Init()
{
// Get existing open window or if none, make a new one:
ObjClone window = (ObjClone)EditorWindow.GetWindow(typeof(ObjClone));
window.Show();
}
GameObject g;
GameObject clone;
volatile bool triggerRename;
private static double renameTime;
void OnGUI()
{
g = (GameObject)EditorGUILayout.ObjectField(g, typeof(GameObject), true);
if (GUILayout.Button("Clone"))
{
clone = Instantiate<GameObject>(g);
EditorApplication.update += EngageRenameMode;
renameTime = EditorApplication.timeSinceStartup + 0.4d;
EditorApplication.ExecuteMenuItem("Window/General/Hierarchy");
Selection.activeGameObject = clone;
}
}
private void EngageRenameMode()
{
if (EditorApplication.timeSinceStartup >= renameTime)
{
EditorApplication.update -= EngageRenameMode;
var e = new Event { keyCode = KeyCode.F2, type = EventType.KeyDown }; // or Event.KeyboardEvent("f2");
EditorWindow.focusedWindow.SendEvent(e);
}
}
}
OBSOLETE:
Ok. Took me some time, but here you go:
using UnityEngine;
using UnityEditor;
using System.Reflection;
public class ObjClone : EditorWindow
{
// Add menu named "My Window" to the Window menu
[MenuItem("Window/ObjClone")]
static void Init()
{
// Get existing open window or if none, make a new one:
ObjClone window = (ObjClone)EditorWindow.GetWindow(typeof(ObjClone));
window.Show();
}
GameObject g;
GameObject clone;
volatile bool triggerRename;
private static double renameTime;
void OnGUI()
{
g = (GameObject)EditorGUILayout.ObjectField(g, typeof(GameObject), true);
if (GUILayout.Button("Clone"))
{
clone = Instantiate<GameObject>(g);
EditorApplication.update += EngageRenameMode;
renameTime = EditorApplication.timeSinceStartup + 0.4d;
EditorApplication.ExecuteMenuItem("Window/Hierarchy");
Selection.activeGameObject = clone;
}
}
private void EngageRenameMode()
{
if (EditorApplication.timeSinceStartup >= renameTime)
{
EditorApplication.update -= EngageRenameMode;
var type = typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow");
var hierarchyWindow = EditorWindow.GetWindow(type);
var rename = type.GetMethod("RenameGO", BindingFlags.Instance | BindingFlags.NonPublic);
rename.Invoke(hierarchyWindow, null);
}
}
}
Not sure since when, but "RenameGO" method no longer exists in SceneHierarchyWindow class. I'm amazed how hard it is to initiate rename in script. Ended up creating an EditorWindow with a text field for that.
Updated answer. The idea behind triggering a rename is to fire a keyboard F2 event. This is usually the shortcut to rename stuff. Works well in Unity 2019.
I tried the F2 solution before, could be something with my setup, but it was inconsistent and didn't work at all unless my cursor was inside the Hierarchy window.