private Rect GetWindowRect(Vector2 windowPosition, float windowHeight) { // If the window width is smaller than the distance from cursor to the right border of the window, the // window will not appear because the cursor is outside of the window and OnGUI will never be called. float screenWidth = EditorDrawHelper.GetScreenWidth(); windowPosition.x -= 8f; // This will make the window appear so that foldout arrows are precisely below the cursor. float distanceToRightBorder = screenWidth - windowPosition.x; if (_optimalWidth > distanceToRightBorder) { distanceToRightBorder = _optimalWidth; windowPosition.x = screenWidth - _optimalWidth; } // If given less than 100f, the window will re-position to the top left corner. If given 0f on MacOS, // the window may not appear at all. Thus, the minimal value is 100f. const float minHeightOnStart = 100f; windowHeight = windowHeight < 100f ? minHeightOnStart : windowHeight; float distanceToBottomBorder = EditorDrawHelper.GetMainWindowPosition().yMax - windowPosition.y; if (distanceToBottomBorder < windowHeight) { windowPosition.y = EditorDrawHelper.GetMainWindowPosition().yMax - windowHeight; } var windowSize = new Vector2(distanceToRightBorder, windowHeight); return(new Rect(windowPosition, windowSize)); }
/// <summary> /// Centers the window in the main Unity window. This is not the same as centering a window on screen, /// because the Unity window may not be maximized. /// </summary> /// <param name="window">The window to center.</param> [PublicAPI] public static void CenterOnMainWin(this EditorWindow window) { Rect main = EditorDrawHelper.GetMainWindowPosition(); Rect pos = window.position; float centerWidth = (main.width - pos.width) * 0.5f; float centerHeight = (main.height - pos.height) * 0.5f; pos.x = main.x + centerWidth; pos.y = main.y + centerHeight; window.position = pos; }
/// <summary>Resizes the window to the needed size.</summary> /// <param name="window">The window to change the size of.</param> /// <param name="width">The width to set. If the value is -1f, the width will not be changed.</param> /// <param name="height">The height to set. If the value is -1f, the height will not be changed.</param> /// <exception cref="ArgumentOutOfRangeException"> /// Thrown if width or height are negative numbers and not -1f. /// </exception> /// <example><code> /// public class DummyWindow : EditorWindow /// { /// private void OnCreate(Rect buttonRect) /// { /// var windowSize = new Vector2(100f, 100f); /// ShowAsDropDown(buttonRect, windowSize); /// } /// /// private void OnGUI() /// { /// float optimalWidth = CalculateOptimalWidth(); /// float optimalHeight = Math.Min(_contentHeight, DropdownStyle.MaxWindowHeight); /// this.Resize(optimalWidth, optimalHeight); /// } /// } /// </code></example> [PublicAPI] public static void Resize(this EditorWindow window, float width = -1f, float height = -1f) { EnsureTheValueIsNotNegative(nameof(width), width); EnsureTheValueIsNotNegative(nameof(height), height); bool changeWidth = width != -1f; bool changeHeight = height != -1f; Rect positionToAdjust = window.position; if (changeWidth) { positionToAdjust.width = width; } if (changeHeight) { positionToAdjust.height = height; } window.minSize = new Vector2(changeWidth ? width : window.minSize.x, changeHeight ? height : window.minSize.y); window.maxSize = new Vector2(changeWidth ? width : window.maxSize.x, changeHeight ? height : window.maxSize.y); if (changeWidth) { float screenWidth = EditorDrawHelper.GetScreenWidth(); if (positionToAdjust.xMax >= screenWidth) { positionToAdjust.x -= positionToAdjust.xMax - screenWidth; } } if (changeHeight) { // MainWindow is more reliable than Screen.currentResolution.height, especially for the multi-display setup. float mainWinYMax = EditorDrawHelper.GetMainWindowPosition().yMax; if (positionToAdjust.yMax >= mainWinYMax) { positionToAdjust.y -= positionToAdjust.yMax - mainWinYMax; } } window.position = positionToAdjust; }