- Home /
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 :)
Good question - I'm dealing with exactly the same problem at the moment. I'd upvote if I could.
Personally, I didn't find a solution, and since then stopped using Unity's GUI (which anyway is a system hog) :P
I've got an answer to this question, although I'd like to ask what did you switch to? NGUI?
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/).
Answer by vigrid · Dec 07, 2012 at 09:15 PM
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.
Answer by vigrid · Dec 07, 2012 at 08:38 PM
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.
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.
P.S. ah, never realized there was a "convert comment to answer" button :P
Answer by belvita · Nov 29, 2013 at 04:02 PM
do not use GUILayout it is a waste of time .try this
private void OnGUI()
{
GUI.Box(new Rect(Screen.width / 2-75, Screen.height / 2-160, 200, 100), "");
//if the button is pressed means if exists
if(GUI.Button(new Rect (Screen.width / 2-75,Screen.height / 2-160,30,30),sand))
{
}
// Make the second button
GUI.Button(new Rect (Screen.width / 2-45,Screen.height / 2-160,30,30),"");
}
}
Answer by Robacks · Nov 29, 2013 at 12:07 AM
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.
When you use "EditorGUILayout" inside your "WindowFunction", this workaround will make all controll positions performed inside "WindowFunction" invalide. Therefore this solution is not valide for a proper functional gui extension.
Answer by Robacks · Nov 29, 2013 at 12:04 AM
I found a working aproach, which does not currupt the "EditorGUILayout" inside your "WindowFunction":
lockRect = GUILayout.Window(0, lockRect, WindowFunction, "Map Editor Modes");
lockRect.x = (Screen.width*0.5f - lockRect.width*0.5f);
this will work with "lockRect" beeing a class member variable.
Your answer
Follow this Question
Related Questions
GUI.Window function not called anymore 1 Answer
using c# script to open/close GUI window 1 Answer
GUILayout not working for me C# 1 Answer