How do I center a GUI Group in the exact middle of the screen regardless of the resolution of the screen?
You can get the centre point of the screen by dividing the width and height properties of the Screen class by 2. Then, you need to offset the X and Y positions of the group rectangle by half of its width and height, respectively:-
var groupWidth: float;
var groupHeight: float;
function OnGUI() {
var halfX = Screen.width * 0.5;
var halfY = Screen.height * 0.5;
var groupRect = new Rect(halfX - groupWidth * 0.5, halfY - groupHeight * 0.5, groupWidth, groupHeight);
GUI.BeginGroup(groupRect);
...
}
So for “halfX” I put the half of the width for the group I want, and same with “halfY” for height, right?
But it doesn’t work, only the upper left quarter of my group shows up. And there’s no indication of it being in the center of the screen.
Oh wait, my mistake. Your code was good as is, those halfX and halfY are the vars ofcourse ![]()
Sorry, still sleepy. It works now, thank you.