Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity Editor keyboard shortcut to close active undocked window
using UnityEngine;
using UnityEditor;
using System.Reflection;
public static class CloseActiveWindow
{
[MenuItem("Edit/Close Active Window ^w", false, -101)]
public static void CloseWindow()
{
var window = EditorWindow.focusedWindow;
if (window != null)
{
// check if window is undocked: https://answers.unity.com/questions/62594/is-there-an-editorwindow-is-docked-value.html
BindingFlags fullBinding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
MethodInfo isDockedMethod = typeof(EditorWindow).GetProperty("docked", fullBinding).GetGetMethod(true);
if ((bool)isDockedMethod.Invoke(window, null) == false)
{
window.Close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment