x


How can i make a GUI Label or Windows in absolute Middle?

How can i make a GUI Label or Windows in absolute Middle?

in Javascript please ;-)

Thanks ;-)

more ▼

asked Apr 12 '10 at 02:32 PM

geilman gravatar image

geilman
1 2 2 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

I'm not sure what you want your label in the middle of, but I'm assuming your talking about the screen. In that case, try this script, which will put a window in the center of the screen:

var windowRect : Rect = Rect (Screen.height - (Screen.height/2), Screen.width -      (Screen.width/2), 100, 30);

function OnGUI () {
    windowRect = GUI.Window (0, windowRect, WindowFunction, "My Window");
}

function WindowFunction (windowID : int) {
    // Draw any Controls inside the window here
}

That should do the trick, but correct me if I'm wrong!

Also, here's a script to put a plain box in the center of the screen:

function OnGUI () {
    GUI.Label (Rect (Screen.height - (Screen.height/2), Screen.width -      (Screen.width/2), 100, 30), "Hi. I'm a plain box.");
}

Hope this helps!

more ▼

answered Apr 12 '10 at 02:50 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

Right but i need maybe a Windows or a Label White Border ok?

Apr 12 '10 at 03:25 PM geilman

Oh. I see what you mean. Hang on, I'll update the answer in a sec.

Apr 12 '10 at 04:55 PM e.bonneville

That won't center the window, though. You need to subtract half the width/height of the window from the x/y coordinates, otherwise the top left of the window will be in the center of the screen, rather than the center of the window.

Two more points: 1) I think your left and top positions are flipped in your rects, and 2) Why are you subtracting half the screen from the left and top? Top is 0 in Unity's GUI, so you can just use Rect ((Screen.width / 2) - (contentWidth / 2), (Screen.height / 2) - (contentHeight / 2), contentWidth, contentHeight)

Apr 12 '10 at 08:22 PM burnumd
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5088
x3695

asked: Apr 12 '10 at 02:32 PM

Seen: 2869 times

Last Updated: Apr 12 '10 at 02:32 PM