This is some what related to this question - but not about sending keys, but about the functionality I wanted from sending the keys.
I’m still looking for a way to engage in rename mode in the hierarchy via code - maybe get an access to the context menu that appears (which has Rename, Duplicate, Copy, Paste, etc)
So I got close this time, with the help of @Jamora, he told me about EditorGUIUtility.CommandEvent - All I needed to do now, is to find a way to give focus to the hierarchy window, I managed to do this via executing the menu item “Window/Hiearchy” - And indeed, I managed to access those options!
var go = new GameObject("[Generated] Temp");
Utils.SelectObject(go);
EditorApplication.ExecuteMenuItem("Window/Hierarchy"); // focus on the hierarchy window
EditorWindow.focusedWindow.SendEvent(EditorGUIUtility.CommandEvent("Duplicate")); // send an event, to the current focused window, which is the hierarchy at this point...
(All SelectObject
does is Selection.objects = new Object[] { obj };
)
Duplicate, Copy, Paste, etc all of these work, except for Rename! Give me a break!
For some reason it just won’t work, nothing happens when I send a Rename, the hierarchy gets focus and,… nothing else.
I tried “Rename”, “rename”, “RENAME” no dice.
Any ideas why rename mode isn’t engaging (while it should)?
Thanks!
EDIT:
Thanks to @ArkaneX - I was able to send a rename event, but for some reason sending the event immediately after I create a new GO, doesn’t seem to do anything (doesn’t engage rename mode):
if (GUILayout.Button("Create")) {
var go = new GameObject("[Generated] New GO");
Utils.EngageRenameMode(go);
}
Where:
public static void EngageRenameMode(Object go)
{
SelectObject(go);
GetFocusedWindow("Hierarchy").SendEvent(Events.Rename);
}
public static void SelectObject(Object obj)
{
Selection.objects = new Object[] { obj };
}
public static EditorWindow GetFocusedWindow(string window)
{
FocusOnWindow(window);
return EditorWindow.focusedWindow;
}
public static void FocusOnWindow(string window)
{
EditorApplication.ExecuteMenuItem("Window/" + window);
}
public static class Events
{
public static Event Rename = new Event() { keyCode = KeyCode.F2, type = EventType.KeyDown };
}
Again, this only doesn’t seem to work when I’m creating a new GO and then renaming. Not sure why…