- Home /
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.
Is it possible to draw icons?
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...
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.
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...
this looks like a fairly complete article and does not cover adding icons.
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 string
s for values.
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
