Last active
June 22, 2022 15:32
-
-
Save josephan/e28cd5b65003627862d774d196fbcd77 to your computer and use it in GitHub Desktop.
Unity Editor keyboard shortcut to close active undocked window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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