- Home /
It's possible to create MenuItems with out attributes?
Hi,
I wan't to know if it's possible to create a Menu Item without using it's attribute. For example, I want to know if there is a possible way to make it for example by calling a method:
EditorMenu.CreateNewMenu(const string menu_name, Action method_to_execute);
I'm saying it, in a theorical way, but I don't know really if this is possible. And the Unity Docs doesn't say anything.
Thanks in advance!
Answer by Bunny83 · Aug 24, 2016 at 10:59 AM
No, because Unity doesn't support adding / removing menu items dynamically at the moment. So the only way currently is to use the MenuItem attribute. The menu items will be "created" / "removed" when the scripting environment is reloaded which happens after each recompile.
edit
I just saw that question again and thought i should add some more information ^^.
While adding / removing native menu items is not supported by the editor, you could of course create your own "submenu" using an EditorWindow. You can show it borderless as tool window or popup. Simply close it when it looses focus. So you would have a static menuitem which just opens your "custom menu".
Another solution could be to use a GenericMenu which you open when you click on a static MenuItem.
The biggest problems with both approaches would be to determine where your menu should be positioned. However here some WinAPI magic could help a bit.
However, just using WinAPI to "inject" custom menus is going to be quite difficult. You most likely should be able to use the Menu API of windows to access and change / add a menu item. However the main problem is to actually get the window messages when an item is clicked. It should theoretically be possible to just change an existing menuitem with that API. To actually handle completely custom menu items you would need to hook into the message queue of the Unity application window. Installing hooks is a topic of it's own. It has to be done using a native DLL with the same architecture as the target application (32 / 64 bit).
I think thats a bit too much just to "add some menu items" into the main menu. It could also break or confuse the Unity application itself if you tamper with it's menu. I wouldn't recommend this. Using an EditorWindow or a GenericMenu is much safer and is cross-platform (in case someone uses mac or linux)
Ps: in case you want to play with the win API, here's how the unity editor window structure looks like. The output here isWindowClassName(#HWND) - Window title
.
Note that the "UnityContainerWndClass" is the windowclass for both, the main window as well as all other floating container windows (when you undock a tab). So finding the main window using FindWindow would not work very well. EnumWindows is usually the better solution.
I will use System.IO to generate custom menus by creating a .cs file dynamically.
Your answer
Follow this Question
Related Questions
MenuItem toggle getting reset on play. 0 Answers
Repaint EditorSettings inspector from custom menu item 1 Answer
How do I add a MenuItem to Hierarchy's Create Menu? 2 Answers
Editor Scripting Question 1 Answer
Can I create MenuItem from code? 3 Answers