Hi all,
I am currently putting together some editor tools, and have come across a minor issue.
After creating an instance of a class that inherits from EditorWindow, I set the position and size of the window by creating a Rect that would place the window in the center of the screen.
public void SetWindowSizeAndPosition()
{
var desired_window_size = kDesiredWindowSize; // Vector2Int(768, 512)
// Create a Rect of the above size, centered on the screen
var window_position = EditorHelper.GetCenteredWindowRect(
desired_window_size.x,
desired_window_size.y
);
position = window_position;
// Set min/max size so window cannot be resized
var fixed_size = new Vector2(window_position.width, window_position.height);
minSize = maxSize = fixed_size;
}
public static Rect GetCenteredWindowRect(int window_width, int window_height)
{
var nullable_out_rect = RectFactory.Make(0, 0, 0, 0);
if (!nullable_out_rect.HasValue)
{
// OOM error or other fatal error.
return new Rect();
}
var out_rect = nullable_out_rect.Value;
// Gets the resolution of the monitor on which the editor is open with
// Screen.currentResolution
var screen_size = GetScreenResolution();
// Scales the screen size down based on the Windows display scaling
// For example, a 4K monitor with a 150% scaling setting will result in
// a resolution of 2560 x 1440, which is the resolution at which the Unity
// editor is actually rendered at
screen_size.x = RawToTrueSize(screen_size.x);
screen_size.y = RawToTrueSize(screen_size.y);
// TODO: Handle the case when Unity is not open on the main monitor
//
// Set the width and height of the window, clamping the values to ensure
// that the window is not wider than the screen
out_rect.width = Mathf.Clamp(window_width, 0, screen_size.x);
out_rect.height = Mathf.Clamp(window_height, 0, screen_size.y);
// Set the x and y position of the window, clamping the value to ensure
// that the window is placed within the bounds of the screen
out_rect.x = Mathf.Clamp((screen_size.x / 2f) - (window_width / 2f), 0, screen_size.x);
out_rect.y = Mathf.Clamp((screen_size.y / 2f) - (window_height / 2f), 0, screen_size.y);
return out_rect;
}
EditorHelper.GetCenteredWindowRect returns exactly that. In the example above, I am creating a window of size [ 768, 512 ] pixels, taking into account the scaling of my 4k monitor.
With the above code, and when I have Unity open on my main monitor, opening the aforementioned EditorWindow works as expected.
However, when I move Unity to my second monitor, and attempt to open the window, it opens on my main monitor - sized and positioned as if it was placed on my second monitor.
It looks like the position value needs to be relative to the main monitor, or perhaps the left-most monitor? For example, when Unity is on my second monitor (1080p), and value of “x” for the Rect that is applied to “position” is 320, it looks like it would in fact have to be <width of main/left-most monitor> + 320.
If my assumptions are correct, in order to correctly calculate the x and y component of the Rect used to set the window position I would need to first determine:
- The position of the monitor on which Unity is currently open (even if its overlapping two screens, it looks like one of them is considered the “current”) relative to the main monitor.
- The size of the main\left-most monitor, and any other monitors between the that monitor and the one on which Unity is currently open.
Is there a way to get this information while in the editor? I have checked the Display and Screen classes but there doesn’t seem to be anything of the sort.
Or am I barking up the wrong tree, and there is some sort of handy Editor tool that can handle this for me?
Kind regards,
James