Search Unity

Question Obtaining scaled display size (+ multi monitor offset) for editor code

Discussion in 'Editor & General Support' started by JasonC_, Jun 3, 2021.

  1. JasonC_

    JasonC_

    Joined:
    Oct 27, 2019
    Posts:
    66
    I want to center some custom editor windows on the screen. I'm having trouble getting the correct display dimensions. Consider this general pseudo-ish code:

    Code (CSharp):
    1. int DialogWidth = 500;
    2. int DialogHeight = 400;
    3. int DisplayWidth = ...;
    4. int DisplayHeight = ...;
    5.  
    6. // And desired window rect is calculated as:
    7. _ = new Rect((DisplayWidth - DialogWidth) / 2,
    8.              (DisplayHeight - DialogHeight) / 2,
    9.              DialogWidth,
    10.              DialogHeight);
    So, for the display dimensions, my initial naïve attempt was:

    Code (CSharp):
    1. int DisplayWidth = Screen.width;
    2. int DisplayHeight = Screen.height;
    Of course that didn't work because that's the size of the game area, not the actual display. So then I tried:

    Code (CSharp):
    1. int DisplayWidth = Screen.currentResolution.width;
    2. int DisplayHeight = Screen.currentResolution.height;
    That still wasn't right but, my system scaling is set to 250% and the editor is set to use system scaling, so hypothesizing that scaling was the issue I tried:

    Code (CSharp):
    1. int DisplayWidth = (int)(Screen.currentResolution.width / 2.5);
    2. int DisplayHeight = (int)(Screen.currentResolution.height / 2.5);
    And it worked perfectly, confirming scale factor as the issue.

    But that's not a good solution, I need to be able to do that calculation without hard coding a scale factor so that it works in any environment. So, my first question is:

    Q: How can I get the scaled display size in an editor script?

    The other thing is, I want this to also work on multiple monitor setups, but I don't actually have a setup right now to experiment with. So the second question is:

    Q: How can I get the offset and scaled display size for the monitor that the Unity Editor is currently being displayed on?

    Thanks!

    Note: I am actually also interested in other ways to center dialogs; but regardless I would like to find out how to get the correct display size in case I ever need it for something else some day (plus I'd still be curious). So getting display size is still my primary question, even if I don't need it for centering dialogs. :)