Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved] Left Click with GenericMenu

Discussion in 'Immediate Mode GUI (IMGUI)' started by DomDomDom, Aug 27, 2016.

  1. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    I've been looking for a way to left click with the Generic Menu. Ideally I'll be doing this on a button element. I so far haven't been able to find anything with left-clicks. If anyone has any tips or tricks on how to do this that would be great!

    Thanks in advance!
     
  2. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Managed to find a solution after some more digging (still learning GUI via scripting). I ended up combining a few different event types with the GenericMenu to get what I needed. See a modified example from the GenericMenu page below. Hope this helps someone else!

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. // This example shows how to create a context menu inside a custom EditorWindow.
    6. // left-click the green area to show the menu
    7.  
    8. public class GenericMenuExample : EditorWindow
    9. {
    10.  
    11.     [MenuItem("Example/Open Window")]
    12.     static void Init()
    13.     {
    14.         EditorWindow window = GetWindow<GenericMenuExample>();
    15.         window.position = new Rect(50, 50, 250, 60);
    16.         window.Show();
    17.     }
    18.  
    19.     void Callback(object obj)
    20.     {
    21.         Debug.Log("Selected: " + obj);
    22.     }
    23.  
    24.     void OnGUI()
    25.     {
    26.         Event currentEvent = Event.current;
    27.         Rect contextRect = new Rect(10, 10, 100, 100);
    28.         EditorGUI.DrawRect(contextRect, Color.green);
    29.  
    30.         if (currentEvent.button == 0 && currentEvent.isMouse)
    31.         {
    32.             Vector2 mousePos = currentEvent.mousePosition;
    33.             if (contextRect.Contains(mousePos))
    34.             {
    35.                 // Now create the menu, add items and show it
    36.                 GenericMenu menu = new GenericMenu();
    37.                 menu.AddItem(new GUIContent("MenuItem1"), false, Callback, "item 1");
    38.                 menu.AddItem(new GUIContent("MenuItem2"), false, Callback, "item 2");
    39.                 menu.AddSeparator("");
    40.                 menu.AddItem(new GUIContent("SubMenu/MenuItem3"), false, Callback, "item 3");
    41.                 menu.ShowAsContext();
    42.                 currentEvent.Use();
    43.             }
    44.         }
    45.     }
    46. }