What is the intended way of showing a context popup window that is invoked via keypress with the new ShortcutManager/ShortcutAttribute system?
For example, this code:
using UnityEditor;
using UnityEditor.ShortcutManagement;
using UnityEngine;
public class Test
{
[Shortcut("Test", KeyCode.G)]
public static void ShowMyCustomPopup()
{
// This GenericMenu shows, but it's not under the mouse,
// becaues we're not calling it from OnGUI.
var menu = new GenericMenu();
menu.AddItem(new GUIContent("Test"), false, null);
menu.ShowAsContext();
// This shows as well, but the mouse position is incorrect,
// since not called from OnGUI.
// Additionally, an unhandled ExitGUIException is thrown
// when closing the popup.
var rect = new Rect(Event.current.mousePosition, Vector2.zero);
PopupWindow.Show(rect, new CustomContent());
}
private class CustomContent : PopupWindowContent
{
public override void OnGUI(Rect rect)
{
EditorGUI.LabelField(rect, "Test");
}
}
}
I’d like to show a popup when a certain key is pressed, but the functions are invoked by Unity not during the OnGUI call, so common GUI functions such as GenericMenu.ShowAsContext or PopupWindow do not work. Is there a way of telling the shortcut system that I want my callback to be invoked in the GUI loop or do I have to queue my own commands and execute them during the next GUI call? If so, how do I get a globally available GUI call? In this specific case I would want my menu to be available in the SceneView and the Hierarchy Window, so I would need to subscribe to multiple GUI loops in this case.
I’ve experimented with using my own GUI callback like this, but it’s not exactly what I want:
using UnityEditor;
using UnityEditor.ShortcutManagement;
using UnityEngine;
public class Test
{
[Shortcut("Test", typeof(SceneView), KeyCode.G)]
public static void ShowMyCustomPopup(ShortcutArguments args)
{
SceneView.beforeSceneGui += OnSceneGUI;
}
private static void OnSceneGUI(SceneView obj)
{
SceneView.beforeSceneGui -= OnSceneGUI;
var rect = new Rect(Event.current.mousePosition, Vector2.zero);
PopupWindow.Show(rect, new CustomContent());
}
private class CustomContent : PopupWindowContent
{
public override void OnGUI(Rect rect)
{
EditorGUI.LabelField(rect, "Test");
}
}
}
When the mouse is over the SceneView I can invoke the shortcut callback and then wait for the next SceneView.beforeSceneGui callback to show the menu. This works nicely, but I also want the shortcut to work in other windows or even globally. However, I don’t have access to the type of the HierarchWindow, so I can’t use it as a custom shortcut context and not all windows have gui callbacks, which I can access.