x


Having a GUILayout Window position immediately at screen center

Hello,

I need to have a GUILayout Window appear at the exact center of the screen, and I'm using this (where winRect is a stored Rect variable):

winRect = GUILayout.Window( winId, winRect, WindowFunction, "Window Title" );
winRect.x = (int) ( Screen.width * 0.5f - winRect.width * 0.5f );
winRect.y = (int) ( Screen.height * 0.5f - winRect.height * 0.5f );

Problem is, the window will be correctly positioned only since the second time it's drawn (because the first time the window rect has not yet been generated), and thus it will appear for an instant in an incorrect position.

Anyone knows a way to reposition a window AFTER GUILayout.Window has been called, but BEFORE it is actually drawn? I suppose this is not possible, but: any workarounds? Please note that I'm talking about liquid/GUILayout windows, where width and height is not known until they're created.

Thanks for any help :)

more ▼

asked Dec 01 '11 at 03:27 PM

Izitmee gravatar image

Izitmee
1.3k 19 22 42

Good question - I'm dealing with exactly the same problem at the moment. I'd upvote if I could.

Dec 07 '12 at 08:07 PM vigrid

Personally, I didn't find a solution, and since then stopped using Unity's GUI (which anyway is a system hog) :P

Dec 07 '12 at 08:14 PM Izitmee

I've got an answer to this question, although I'd like to ask what did you switch to? NGUI?

Dec 07 '12 at 09:12 PM vigrid

I heard lots of good stuff about NGUI, so I'd recommend it even if I never used it (alos, its developer was just hired by Unity to work on the new version of Unity GUI). Personally, since I have and love 2D Toolkit, I built a framework to easily use it also for the GUI (by the way, I'm remaking it from scratch and posted it on Google Code among my HOUnityLibs, in case you're interested - though for now it's good only for buttons: http://code.google.com/p/hounitylibs/).

Dec 07 '12 at 09:23 PM Izitmee
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Calling `GUILayout.Window` again repositions the window without any flicker, nor extra draw calls.

winRect = GUILayout.Window( winId, winRect, WindowFunction, "Window Title" );
winRect.x = (int) ( Screen.width * 0.5f - winRect.width * 0.5f );
winRect.y = (int) ( Screen.height * 0.5f - winRect.height * 0.5f );

// Added
GUILayout.Window( winId, winRect, WindowFunction, "Window Title" );

Rationale: My guess is Unity doesn't actually draw the Window, but manages its position in this call, while building a draw call list to be sent. Calling this again invalidates the list created previously for that window.

more ▼

answered Dec 07 '12 at 09:15 PM

vigrid gravatar image

vigrid
68 1 4

I like this one even more! Marked :)

Dec 07 '12 at 09:19 PM Izitmee
(comments are locked)
10|3000 characters needed characters left

Is it? Can you explain how is it a system hog? Also, a workaround I would do - I would draw the GUI on the first frame it pops up with Alpha set to zero.

more ▼

answered Dec 07 '12 at 08:38 PM

vigrid gravatar image

vigrid
68 1 4

Smart workaround. If you write it as a complete answer, I will vote it as the correct one :) About Unity GUI being a system hog, you can check anywhere on the internet, since everybody agrees: especially for mobile development it is definitely un-performant.

Dec 07 '12 at 08:53 PM Izitmee

P.S. ah, never realized there was a "convert comment to answer" button :P

Dec 07 '12 at 09:25 PM Izitmee
(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:

x4376
x3816
x208
x144

asked: Dec 01 '11 at 03:27 PM

Seen: 1210 times

Last Updated: Dec 07 '12 at 09:25 PM