Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Why does UIElements.DropdownMenu not derive from VisualElement?

Discussion in 'UIElements' started by mikevargas, Nov 6, 2019.

  1. mikevargas

    mikevargas

    Joined:
    Aug 17, 2019
    Posts:
    15
    I was so thrilled to find what appeared to be a simple, generic DropdownMenu control for UIElements:
    https://docs.unity3d.com/2019.3/Documentation/ScriptReference/UIElements.DropdownMenu.html

    But as it turns out, confusingly, this is not a VisualElements-derived class, so I cannot add it to my visual hierarchy. Clearly I am not alone in my confusion, as someone else asked the same question: https://answers.unity.com/questions/1650583/visualelement-how-to-add-unityengineuielementsdrop.html

    How can I add this to my custom editors? And if I can't, what is the appropriate control to use for a simple dropdown menu?
     
  2. alexandred_unity

    alexandred_unity

    Unity Technologies

    Joined:
    Dec 12, 2018
    Posts:
    3
    Hi,
    Here is a quick example of how UIElements.DropdownMenu can be used.


    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.UIElements;
    4. using UnityEditor.UIElements;
    5.  
    6. class MyButtonWithMenu : TextElement
    7. {
    8.     public DropdownMenu menu { get; private set; }
    9.  
    10.     public MyButtonWithMenu()
    11.     {
    12.         menu = new DropdownMenu();
    13.         this.AddManipulator(new PointerClickable(e => menu.DoDisplayEditorMenu(this.worldBound)));
    14.     }
    15. }
    16.  
    17. public class MyMenuTest : EditorWindow
    18. {
    19.     [MenuItem("Window/MyMenuTest")]
    20.     public static void ShowExample()
    21.     {
    22.         MyMenuTest wnd = GetWindow<MyMenuTest>();
    23.         wnd.titleContent = new GUIContent("MyMenuTest");
    24.     }
    25.  
    26.     private MyButtonWithMenu m_ButtonWithMenu;
    27.     private int m_ActiveCount = 0;
    28.  
    29.     public void OnEnable()
    30.     {
    31.         // Each editor window contains a root VisualElement object
    32.         VisualElement root = rootVisualElement;
    33.  
    34.         m_ButtonWithMenu = new MyButtonWithMenu();
    35.  
    36.         m_ButtonWithMenu.menu.AppendAction("Increase Count", a => { m_ActiveCount++; UpdateText(); }, a => DropdownMenuAction.Status.Normal);
    37.         m_ButtonWithMenu.menu.AppendAction("Decrease Count", a => { m_ActiveCount--; UpdateText(); }, a => DropdownMenuAction.Status.Normal);
    38.  
    39.         m_ButtonWithMenu.style.color = Color.blue;
    40.         m_ButtonWithMenu.style.backgroundColor = Color.red;
    41.         m_ButtonWithMenu.style.paddingTop = 4;
    42.         m_ButtonWithMenu.style.paddingBottom = 4;
    43.         m_ButtonWithMenu.style.paddingLeft = 4;
    44.         m_ButtonWithMenu.style.paddingRight = 4;
    45.  
    46.         root.Add(m_ButtonWithMenu);
    47.         UpdateText();
    48.     }
    49.  
    50.     void UpdateText()
    51.     {
    52.         m_ButtonWithMenu.text = "Click Me Please! Count = " + m_ActiveCount;
    53.     }
    54. }
    55.  
     
    Last edited: Nov 8, 2019
    mikevargas and mbussidk like this.
  3. mikevargas

    mikevargas

    Joined:
    Aug 17, 2019
    Posts:
    15
    This looks like an excellent solution, I'm sorry I didn't notice the reply until now!

    I don't seem to see a "PointerClickable" manipulator defined anywhere, though, even though I can see it when searching the Unity reference source.

    I also don't see the DoDisplayEditorMenu extension method, which I can find in the reference source but seems inaccessible from my project. I'm running 2019.3.0b7, which is newer than builds which apparently had this functionality. Am I missing something obvious? I'm including the proper using statements (UnityEditor.UIElements and UnityEngine.UIElements, etc.).
     
    Last edited: Dec 3, 2019 at 1:03 AM