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
5
Question by Jessy · Mar 27, 2010 at 10:06 PM · editor-scriptingmenumenu-item

Can I make the name of a custom Menu Item context-sensitive, or show/hide it dynamically?

Can I change singular to plural, make the word "On" turn to "Off" when appropriate, etc., for a single function?

Alternatively, dynamically including and removing menu items as various states change, would be fine. Can that be done?

Comment
Add comment · Show 4
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 e-bonneville · Mar 27, 2010 at 10:12 PM 0
Share

Complex, and in my unworthy opinion, unnecessary, but surely possible.

avatar image Peter G · Jan 27, 2011 at 10:52 PM 1
Share

I don't believe you can. Since attributes require constant variables as parameters, your variable (probably a string) would have to be decided at compile-time, not runtime, and you couldn't change it.

avatar image Bunny83 · Jan 27, 2011 at 11:02 PM 0
Share

I think you can't change the itemName of the attribute class because it seems the name is stored inside in a private variable. But you should be able to use Reflection to remove the attribute from that function and add a new one. $$anonymous$$aybe you can derive your own attribut class from $$anonymous$$enuItem, but i still think the name stored in a private var and not a protected one.

Furthermore I doubt that Unity will recognise the changes because i believe Unity itself use Reflection to check for those attributes at compilation time and will build the menu with this information. But would be nice to know ;)

avatar image yoyo · Jan 29, 2011 at 10:09 PM 1
Share

You could implement your own editor window with a separately managed set of menus.

1 Reply

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

Answer by jashan · Jan 28, 2011 at 08:50 AM

As you define the name of menu items via an attribute (MenuItem), I don't see a way of changing the name of the menu dynamically. However, what you can do is have two entries and enabling/disabling them depending on context. This may not really make sense with singular / plural but it certainly makes sense for the on/off case (which is also what I'm actually using this for).

Simply use the second constructor of menu item from the documentation (this is C#, JavaScript can be found in the docs ;-) ):

// Validate the menu item.
// The item will be disabled if no transform is selected.
[MenuItem("GameObject/Do Something", true)]
static void ValidateDoSomething () {
    return Selection.activeTransform != null;
}

So, you need two methods: One that actually defines the functionality of your menu item (that will just use MenuItem("Name")), and another one that checks for the context (that one will use MenuItem("Name", true") and return true when the menu item should be enabled and false if it should be disabled.

Comment
Add comment · Show 4 · 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 Jessy · Jan 28, 2011 at 02:22 PM 0
Share

Oh, I use validation functions on pretty much all of my menu items. But although graying out and taking away the ability to click on or use a keyboard shortcut is nice, the words still remain. What I want is something more dynamic. For example, open any of Apple's apps, click on the "Window" menu, and tap the Option key. $$anonymous$$aybe the idea is that two different functions are called; I don't know. That would be just fine too and I'll edit the question to reflect that.

avatar image bioduds · Jun 03, 2012 at 06:06 PM 0
Share

void static void ???????!!!!!!! sorry, don't compute!!

avatar image Bunny83 · Jun 07, 2012 at 01:30 AM -1
Share

@bioduds: fixed it ;)

avatar image DESTRUKTORR · Jun 14, 2014 at 02:00 AM 2
Share

Your void is returning a bool... That wouldn't compile :P

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

1 Person is following this question.

avatar image

Related Questions

Separator in custom Editor MenuItem 1 Answer

Editor Scripting Question 1 Answer

How do I make a level editor in the editor 1 Answer

How would you go about showing an editor preferences panel in the inspector? 1 Answer

How to make watch an Ad to continue the game if player is Dead 0 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