Skip to content

Instantly share code, notes, and snippets.

@josephan
Last active June 22, 2022 15:32
Show Gist options
  • Save josephan/e28cd5b65003627862d774d196fbcd77 to your computer and use it in GitHub Desktop.
Save josephan/e28cd5b65003627862d774d196fbcd77 to your computer and use it in GitHub Desktop.
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