Created
October 7, 2021 21:25
-
-
Save mminer/f91c0387d025acb88d4f013ece9412ee to your computer and use it in GitHub Desktop.
Unity function to get the main editor window's position.
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
// This contains no error checking, which you want for such fragile code. | |
// You also probably want to cache `mainWindow` if you call this often. | |
static Rect GetMainWindowPosition() | |
{ | |
var containerWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ContainerWindow"); | |
var showModeField = containerWindowType.GetField("m_ShowMode", BindingFlags.Instance | BindingFlags.NonPublic); | |
var mainWindow = Resources.FindObjectsOfTypeAll(containerWindowType).First(window => (int)showModeField.GetValue(window) == 4); | |
var positionProperty = mainWindow.GetType().GetProperty("position", BindingFlags.Instance | BindingFlags.Public); | |
return (Rect)positionProperty.GetValue(mainWindow, null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment