Context menu In Custom IMGUI Unity Editor Window

I am trying to write a custom editor window. The UI Toolkit is giving me problems so I am using the IMGUI. I need to make a Context menu when I right click but I can not seem to get it to work. I am currently using a “GenericMenu” for the menu, but I can not get it to only open when I right click on the editor window.

Thank you!

You probably just need to use the Event class in the Editor window’s OnGUI and check for any right-click inputs.

Also, what troubles was UI Toolkit giving you?

I am having troubles understanding how to edit the nodes/transitions in the graph view. I want the transitions to have special properties. I am also having this problem which slowing down production a whole lot. If I try Input.GetMouseButtonDown(1) it does not work. I think it is because I am not in play mode. If I check for right mouse button down would it return true when I am looking in a different part of the editor? Thank you for your fast response.

Well that does seem annoying. I haven’t experienced that though I’m working in 2023.2, also on Windows 10 as opposed to 11 (I heard about too many graphical issues so have stayed away for now).

Yes any of that input is only really designed for runtime use.

Like I said, you need to use the Event class to read input in IMGUI in the editor. It’s clunky as hell, though. Which is why I only use UI Toolkit for editor windows these days.

Thank you soo much! The event works! For anyone who want to see the code that worked here is the code

// Creates the Context Menu
GenericMenu menu = new GenericMenu();

// Adds a item to the menu
menu.AddItem(new GUIContent("TEST!"), false, () =>
{

});

// Test to see if the mouse button is down.
if (Event.current.type == EventType.MouseDown && Event.current.button == 1)
{
    Debug.Log("Right Button Down!");
    menu.ShowAsContext();
}