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 mrtkhosravi · Aug 29, 2017 at 04:32 PM · editor-scriptingeditor window

GenericMenu with icons?

Is it possible to have icons in a GenericMenu? Consider the following code:

     GenericMenu menu = new GenericMenu();
     menu.AddItem(new GUIContent("1" , someTex), false, SomeMethod, someObject);

and someTex is a Texture2d. It does not draw an icon beside the menu item.

alt text

Is it possible to draw icons?

000730.png (3.4 kB)
Comment
Add comment · Show 5
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 Hurri04 · May 16, 2018 at 08:33 PM 0
Share

did you ever find a solution for this? sorry to bump this old thread but it's literally the only one I found on this topic.

I've seen that the bool "on" can be used to set a tick to the left of the text but since only very few menu items are designed as toggles or selection groups it seems like a bit of a waste of space to me to not be able to set an icon ins$$anonymous$$d...

avatar image stektpotet Hurri04 · Aug 04, 2018 at 08:50 PM 0
Share

Looking at the decompiled code for Unity: You'll find that no, it's not possible

For some reason they're extracting only the text from from the GUIContent.

I'm considering to write a new Generic$$anonymous$$enu type that also takes use of the textures of the GUIContent. If I do make it I'll be sure to post it here too.

avatar image Hurri04 stektpotet · Aug 05, 2018 at 06:44 AM 0
Share

Yep, I also came across this a while ago when I tried to build my own Generic$$anonymous$$enu as well. (spoiler: ultimately I gave up after wasting a whole day on it, because of certain behaviours and the end-result looking very bland without investing much more time into it.)

 

What I tried was using EditorWindow and PopupWindow (not at the same time but as 2 possibilities) to draw the body of the menu that opens when you press a button in the menu bar (or go into a sub-menu in the currently opened menu via the > arrow on the right).

 

This meant that I'd have to keep track of the menu item tree myself though which was was done easily enough with some recursion where I'd split a path string by by the "/" characters. from the resulting string array I'd create objects of a custom $$anonymous$$enuItem class if there wasnt another object with the same name at the same level in the same brach and insert it as children into a tree graph via the root node. this woud've allowed me to even create a functionality to be able to pass a callback method to a $$anonymous$$enuItem which has sub-menus.

 

While at it I was able to replace the kinda stupid "Generic$$anonymous$$enu.$$anonymous$$enuFunction" and "Generic$$anonymous$$enu.$$anonymous$$enuFunction2" callback parameters with a custom one that would take "params object[] userData" as an argument and supply that via the last parameter of the AddItem method to give the option to pass more than 0 or 1 objects as parameters.

 

The problems I faced though were that with the EditorWindow it was impossible to catch mouse click events when clicking outside it (iirc) and with the PopupWindow that it'd close when clicking in a sub-menu which would've raised the need for me to manually track which path was currently opened, possibly leading to more problems, e.g. with overlapping menu bodies (when opening many sub-menus reaches the right side of the screen and has to open the next one on the left side). I think there was also something about mouse events being sent multiple times which messed with stuff...

 

And while drawing the background in the light grey color was possible with DrawRect, drawing the vertical divider for the icons on the left and the horizontal item separators would've probably required drawing a texture to look good. same for the > arrows, the actual icons of course, and the faint blue highlight of the menu item the cursor is currently over.

 

Then there'd be the menu item text though which by default looks nothing like the one in the current Generic$$anonymous$$enu when drawn with the editor Gui or GuiLayout methods (line height, font size, font style, etc.).

 

So the main problems were the input handling and needing to invest too much time to make it look good. In the end I deleted it all out of frustration without even making a commit of it...

Show more comments
avatar image elenzil · Jan 31, 2019 at 05:31 PM 0
Share

this looks like a fairly complete article and does not cover adding icons.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RudyTheDev · Mar 04, 2019 at 12:46 PM

This is not possible in Unity (as of 2018.3.0f2).

The GUIContent you pass to the AddItem is stored internally, but the method that draws the menu GenericMenu.ObjectContextDropDown only uses GUIContent.text. In other words, Unity ignores texture and tooltip values. Even if one were to try reflection, EditorUtility.DisplayObjectContextPopupMenuWithExtraItems only takes strings for values.

Comment
Add comment · Show 1 · 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 Hurri04 · Mar 04, 2019 at 03:58 PM 0
Share

Which is a shame if you ask me. The "AddItem" method already has the "on" parameter which allows drawing a small tick icon on the left of the text. This means that the underlying functionality is already there, although it's somewhere deep in the engine code...

If there were converns (from the Unity devs) that drawing an icon and the tick on top could cause visual clutter, leading to confusion, this should've still been left open as an option for the devs of editor extensions to decide. especially since the tick is used only very rarely so that having only an icon would work just fine.

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

79 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

Related Questions

Is there a way to enter a text in the search bar of the hierarchy window by an editor script ? 1 Answer

Editor window doesn't open 2 Answers

Multiple Instances of the Same Type of Editor Window 0 Answers

Weird bug when dragging object onto EditorGUILayout.ObjectField 0 Answers

custom fogColor editorWindow undo problem 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