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 :slight_smile:

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.

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.

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),"");

	}
		
	
}

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.

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.