- Home /
Sending a "Rename" CommandEvent to the Hiearchy window, almost there...
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...
It would be nice to have a listener that outputs the commands we use in the editor, like in 3ds. No clue about your question though, sorry.
yeah well I tried to do that - i.e. tried logging Event.current.type
, Event.current.commandName
and sending the Duplicate, Copy, etc commands to the hierarchy, but they're useless unless they're somehow injected to the hierarchy's window code...
Answer by ArkaneX · Feb 20, 2014 at 12:53 PM
Hmmm. There's no Edit\\Rename command in my version of Unity. Are you sure you have this command?
But you can do this using F12. I answered your original question, but posting solution here as well:
var e = new Event { keyCode = KeyCode.F2, type = EventType.keyDown }; // or Event.KeyboardEvent("f2");
EditorWindow.focusedWindow.SendEvent(e);
EDIT: I think the problem might be related to the fact, that adding a game object (internally, by Unity Editor) is a complex process, which involves many steps and requires some time to fully finish. I was able to workaround this, but you have to either seek a way to determine when the object is fully created in editor or live with an arbitrary delay. I used a delay of 0.2s, but for example 0.1s was too low in my environment - you have to experiment.
public class EventsTest : EditorWindow
{
private static GameObject go;
private static double renameTime;
[MenuItem("Test/Events")]
private static void Test ()
{
go = new GameObject("[Generated] New GO");
renameTime = EditorApplication.timeSinceStartup + 0.2d;
EditorApplication.update += EngageRenameMode;
}
private static void EngageRenameMode()
{
if (EditorApplication.timeSinceStartup >= renameTime)
{
EditorApplication.update -= EngageRenameMode;
Utils.EngageRenameMode(go);
}
}
}
FYI, I don't have Rename in Edit menu, but I do have it when I right click an object in the hierarchy window.
I didn't know as well. But out of curiosity checked Event class returned by CommandEvent ;)
Right - there's one in context menu, but I think it is treated differently and is not considered a standard command.
Answer by vexe · Apr 05, 2015 at 06:46 PM
Here's a solution via reflection. This is the same method that gets called when you F2
or Right Click | Rename
.
[MenuItem("Test/Rename")]
public static void Rename()
{
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);
}
Answer by SamOld · Apr 29, 2020 at 11:13 PM
This answer is about engaging rename mode in the Project Window. The question is about the Hierarchy Window. I'm putting it here because it shows up high on google when looking up renaming assets in the project window.
The most common reason for wanting to do this is probably because you're creating a new asset and want the user to be able to edit it. This can now be done using a utility method. UnityEditor.ProjectWindowUtil.CreateAsset(UnityEngine.Object asset, string pathName)
.
The ProjectWindowUtil
class is undocumented, but public. This method appears to handle the whole process of adding the asset to the database, as well as selecting it and going into rename mode just like happens when you use the menu to create an asset.
Your answer
Follow this Question
Related Questions
Detect Enter Key Event with GUILayout.TextField Focused 1 Answer
How can I hide function in Animation Event Window? 0 Answers
EditorWindow not properly drawing 1 Answer
EditorWindow.focusedWindow.SendEvent crashes Unity. 1 Answer
Generic Unity Event triggered from Runtime with listener added in Custom Unity Editor 0 Answers