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!
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!
using UnityEngine;
using UnityEditor;
using System.Collections;
// This example shows how to create a context menu inside a custom EditorWindow.
// left-click the green area to show the menu
public class GenericMenuExample : EditorWindow
{
[MenuItem("Example/Open Window")]
static void Init()
{
EditorWindow window = GetWindow<GenericMenuExample>();
window.position = new Rect(50, 50, 250, 60);
window.Show();
}
void Callback(object obj)
{
Debug.Log("Selected: " + obj);
}
void OnGUI()
{
Event currentEvent = Event.current;
Rect contextRect = new Rect(10, 10, 100, 100);
EditorGUI.DrawRect(contextRect, Color.green);
if (currentEvent.button == 0 && currentEvent.isMouse)
{
Vector2 mousePos = currentEvent.mousePosition;
if (contextRect.Contains(mousePos))
{
// Now create the menu, add items and show it
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("MenuItem1"), false, Callback, "item 1");
menu.AddItem(new GUIContent("MenuItem2"), false, Callback, "item 2");
menu.AddSeparator("");
menu.AddItem(new GUIContent("SubMenu/MenuItem3"), false, Callback, "item 3");
menu.ShowAsContext();
currentEvent.Use();
}
}
}
}