Hi,
I’ve been struggling on this for an hour now.
I have a very basic EditorWindow:
public class ChangeCellSizeWindow : EditorWindow
{
public static void ShowWindow()
{
var window = GetWindow<ChangeCellSizeWindow>();
// Showing it as a modal forces the user to process it immediately
window.ShowModalUtility();
}
}
My problem lies in that last line.
I want to capture keyboard inputs when my window is visible.
From the docs, I read that EditorWindow has already an Update function: Unity - Scripting API: EditorWindow.Update()
BUT that function doesn’t seem to get called when if I specifically show my window as a ModalUtility window, or just as a modal window using window.ShowModal().
If I use window.Show(), or even window.ShowUtility() instead, the Update function is called.
How to get the best of both worlds?
It’s really more relevant for my window to display as a modal window.
I also tried subscribing to some delegates in OnEnable/OnDisable (like SceneView.duringSceneGUI and EditorApplication.update), and it didn’t work (not printing Debug.Log every frame, and not catching inputs from Event.current).
Hi, I found your post when solving similar issue. With new UI Toolkit I can create modal EditorWindow and capture keys in it. Below is stripped down code (loading .uxml/.uss files is commented out).
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class ElementsSelection : EditorWindow {
private VisualElement _rootElement;
private VisualTreeAsset _visualTree;
// ---------------------------------------------------------
public static void CreateEditorWindow() {
ElementsSelection window = GetWindow<ElementsSelection>();
window.minSize = new Vector2(200, 200);
window.titleContent = new GUIContent("Select element");
VisualElement panel = window.rootVisualElement.panel.visualTree;
panel.ElementAt(0).RegisterCallback<MouseDownEvent>((evt) => {
window.GiveFocusToEditor();
});
window.ShowModalUtility();
}
// ---------------------------------------------------------
public void OnFocus() {
GiveFocusToEditor();
}
// ---------------------------------------------------------
public void GiveFocusToEditor() {
rootVisualElement.Focus();
}
// ---------------------------------------------------------
public void OnEnable() {
_rootElement = rootVisualElement;
/*
_visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(EditorConfig.EDITOR_PATH + "ElementsSelectionTemplate.uxml");
StyleSheet styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(EditorConfig.EDITOR_PATH + "ElementsSelectionStyles.uss");
_rootElement.styleSheets.Add(styleSheet);
_rootElement.Clear();
_visualTree.CloneTree(_rootElement);
*/
// make root element focusable
_rootElement.focusable = true;
_rootElement.RegisterCallback<KeyDownEvent>(OnKeyDown);
}
// ---------------------------------------------------------
private void OnKeyDown(KeyDownEvent evt) {
evt.StopImmediatePropagation();
if (evt.keyCode == KeyCode.Escape) {
Close();
}
}
}