• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by vexe · Feb 20, 2014 at 06:24 AM · editorwindoweventrename

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...

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Feb 20, 2014 at 09:07 AM 0
Share

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.

avatar image vexe · Feb 20, 2014 at 11:56 AM 0
Share

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...

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
         }
     }
 }

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Feb 20, 2014 at 12:57 PM 0
Share

Brilliant! I didn't know you could create events like that

avatar image vexe · Feb 20, 2014 at 12:59 PM 0
Share

FYI, I don't have Rename in Edit menu, but I do have it when I right click an object in the hierarchy window.

avatar image ArkaneX · Feb 20, 2014 at 01:00 PM 0
Share

I didn't know as well. But out of curiosity checked Event class returned by CommandEvent ;)

avatar image ArkaneX · Feb 20, 2014 at 01:21 PM 0
Share

Right - there's one in context menu, but I think it is treated differently and is not considered a standard command.

avatar image ArkaneX · Feb 21, 2014 at 11:15 AM 1
Share

Answer updated.

Show more comments
avatar image
1

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);
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Generic Unity Event triggered from Runtime with listener added in Custom Unity Editor 0 Answers

GUI.Window makes custom EditorWindow drag header unresponsive 2 Answers

Event.button activating constantly? 0 Answers

How to get Event.KeyboardEvent from non-focused EditorWindow? 0 Answers

Detecting MouseUp event when the mouse is not over an EditorWindow 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges