Skip to content

Instantly share code, notes, and snippets.

Created October 7, 2021 21:25
What would you like to do?
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