Skip to content

Instantly share code, notes, and snippets.

@mminer
Created October 7, 2021 21:25
Show Gist options
  • Save mminer/f91c0387d025acb88d4f013ece9412ee to your computer and use it in GitHub Desktop.
Save mminer/f91c0387d025acb88d4f013ece9412ee to your computer and use it in GitHub Desktop.
Unity function to get the main editor window's position.
// 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