Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by z3nth10n · Aug 23, 2016 at 03:50 PM · editormenuitem

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!

Comment
Add comment
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

1 Reply

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

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 is
WindowClassName(#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.

Comment
Add comment · Show 2 · 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 z3nth10n · Aug 25, 2016 at 01:02 PM 0
Share

I will use System.IO to generate custom menus by creating a .cs file dynamically.

avatar image z3nth10n · Aug 25, 2016 at 01:04 PM 0
Share

And, using winapi and injecting code in the editor?

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.

Follow this Question

Answers Answers and Comments

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

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


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