Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by Pangamini · Oct 25, 2017 at 10:23 AM · editormenuitem

Can I create MenuItem from code?

I can create MenuItem using the attribute on a static method; I was wondering, is it possible to add menu items in editor code?

Eg. Run through all types of certain basetype through reflection, add creation menuitem for every non-abstract type

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 kalmdown3D · Mar 11, 2020 at 05:19 PM 0
Share

Would like create $$anonymous$$enuItems from code to create a menu of cameras in the scene. I don't really need/want a separate function per camera. Would prefer each menu item could call the function with a param.

avatar image Pangamini kalmdown3D · Mar 11, 2020 at 10:11 PM 1
Share

I think the easiest for you would be to create a custom EditorWindow - then you can fill it anything you want. You can then dock it somewhere in the corner

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Pangamini · Oct 25, 2017 at 11:45 AM

So I made a boo macro that does what I needed. Not exactly adding menuItems in runtime, but at least dynamically during compilation; Since C# doesn't support any kind of macros, here's my example class written in BOO. It has to be placed in Editor folder (so the runtime assemblies are already loaded and can be looked into) This particular case creates a MenuItem creation method for every class that derives from Framework.Entity and isn't abstract.

 namespace Framework.Editor
 
 macro GenerateMenuItems:
     
     for assembly in System.AppDomain.CurrentDomain.GetAssemblies():
         try:
             types = assembly.GetTypes();
         except e:
             continue
 
         for type in types:
 
             baseType = Framework.Entity
             if type.IsAbstract: continue
             if baseType.IsAssignableFrom(type):
 
                 typeName = type.Name;
                 methodName = "MenuItem_$(typeName)"
                 menuPath = "GameObject/Entity/$typeName"
 
                 yield [|
                     [UnityEditor.MenuItem($menuPath)]
                     public static def $methodName():
                         Framework.Entity.CreateEntity_Editor[of $type]()
                         return
                 |]
 
 class EntityCreationMenu:
     GenerateMenuItems


After the compilation, the class (decompiled using ILSpy) looks like this:

 [Serializable]
     public class EntityCreationMenu
     {
         [MenuItem("GameObject/Entity/Entity")]
         public static void MenuItem_Entity()
         {
             Entity.CreateEntity_Editor<Entity>();
         }
 
         [MenuItem("GameObject/Entity/TriggerDelay")]
         public static void MenuItem_TriggerDelay()
         {
             Entity.CreateEntity_Editor<TriggerDelay>();
         }
 
         [MenuItem("GameObject/Entity/TriggerIsDead")]
         public static void MenuItem_TriggerIsDead()
         {
             Entity.CreateEntity_Editor<TriggerIsDead>();
         }
 
         [MenuItem("GameObject/Entity/TriggerOnEnter")]
         public static void MenuItem_TriggerOnEnter()
         {
             Entity.CreateEntity_Editor<TriggerOnEnter>();
         }
 
 //...................... and so on

Comment
Add comment · Show 3 · 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 Pangamini · Oct 25, 2017 at 11:56 AM 0
Share

BTW you can have the test of the project written in C# or any other language

avatar image Bunny83 · Oct 25, 2017 at 02:21 PM 0
Share

Wow, nice feature in boo. Yes, that simplifies the dynamical code generation quite a bit ^^.

ps: Are boo script compiled after the C# scripts? Because "System.AppDomain.CurrentDomain.GetAssemblies" would access the "old" C# assembly otherwise.

edit
Silly me, of course this is an editor script ^^ so it's compiled after the runtime assemblies...

avatar image Pangamini Bunny83 · Oct 25, 2017 at 02:24 PM 0
Share

It won't access the old assembly, because assemblies cannot be unloaded once loaded - the only way (and that's probably what unity is doing) is to $$anonymous$$r down the whole AppDomain and assemble a new one - so in the worst case there would be no assemblies loaded. But I think Boos are loaded after c#s and even if not, this is an editor assembly and those are loaded after runtime assemblies anyway, so no threat there.

Btw the feature is super amazing, you can do similar thing with attributes and access the whole compiled AST, allowing you to create new and modify existing classes, their members, method bodies, anything there is. Such a shame that boo's tool support is so weak

avatar image
1

Answer by Bunny83 · Oct 25, 2017 at 10:53 AM

No that's not possible. Attributes are part of the compiled code. They are simply metadata stored directly in the compiled code, So you can't add MenuItems dynamically. Even if it would be possible since they are just metadata they don't do anything on their own. The Unity editor is searching for the attributes when the code has finished compilation and build the menus based on that metadata. This is only done once after the code has been compiled or a an asset import as been completed.


For such a feature Unity would need to add an API to the editor to query, delete and add menu items dynamically which unfortunately doesn't exist (yet).

Comment
Add comment · Show 3 · 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 Pangamini · Oct 25, 2017 at 10:56 AM 0
Share

Well ofc I wouldn't expect this to be done with attributes in any way. But I guess the answer is that there's no API for that.

I wonder if Unity does this search everytime a new assmebly is loaded ( so that I could hack around this by generating assembly with attributes inside :-P )

avatar image Bunny83 · Oct 25, 2017 at 10:59 AM 0
Share

In theory you could create some sort of post processor code that runs after Unity has finished compilation / importing assets which searches for your target classes and create a seperate auto generated script file which simply holds a static wrapper method and an attribute on it for every class. However you have to make sure you do not update the file every time as writing the file would cause another recompile. So you would need to compare the current methods / attributes with the class list.


If you do something like that it might be better to simply add a menuItem which allows the user to trigger this process manually rather than having it run automatically.

avatar image Pangamini Bunny83 · Oct 25, 2017 at 11:01 AM 0
Share

I think I might hack that using boo, because it supports syntactic macros. That way I can generate these static methods during compilation without modifying any scripts during the process

avatar image
0

Answer by web · Jan 30, 2020 at 10:51 PM

You can't add an arbitrary number of menu items AFAIK, and I'd like to be wrong about that. This, BTW, seems like basic stuff the Editor should have supported from Day 1. Anyway, those strings in the MenuItem attribute don't have to be baked in. They can be static string vars, you can change them programatically. So if you know how many you'll have, you can fill them in on the fly.

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.

Follow this Question

Answers Answers and Comments

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

Repaint EditorSettings inspector from custom menu item 1 Answer

How do I add a MenuItem to Hierarchy's Create Menu? 2 Answers

MenuItem toggle getting reset on play. 0 Answers

Editor Scripting Question 1 Answer

How to use Arrow Key for MenuItem Hotkey 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