Put Gui In middle of screen

Hi i have my gui for changing the controls how would i go about centering it?

using UnityEngine;
using System.Collections;

public class Gui : MonoBehaviour {
	private Vector2 _scrollPosition = new Vector2();
	public bool ShowMenu;
	float native_width = 800;
	float native_height = 600;
	void Update()
	{
		if(Input.GetKeyDown(KeyCode.Escape))
		{
			ShowMenu = !ShowMenu;
		}
	}
	void OnGUI()
	{
		float rx = Screen.width / native_width;
		float ry = Screen.height / native_height;
		GUI.matrix = Matrix4x4.TRS (new Vector3(0f, 0f, 0f), Quaternion.identity, new Vector3(rx, ry, 1f));
		if (ShowMenu == true)
		{
			_scrollPosition = GUILayout.BeginScrollView(_scrollPosition);
			GUILayout.BeginHorizontal();
			
			GUILayout.BeginVertical();
			GUILayout.Label("Action");
			
			for (int n = 0; n < cInput.length; n++) 
			{
				GUILayout.Label(cInput.GetText(n, 0));
			}	
			
			GUILayout.EndVertical();
			GUILayout.BeginVertical();
			GUILayout.Label("Primary");
			for (int n = 0; n < cInput.length; n++) 
			{
				if (GUILayout.Button(cInput.GetText(n, 1)) && Input.GetMouseButtonUp(0)) 
				{
				cInput.ChangeKey(n, 1);
				}
			}
			GUILayout.EndVertical();
			GUILayout.BeginVertical();
			GUILayout.Label("Secondary");
			for (int n = 0; n < cInput.length; n++) 
			{
				if (GUILayout.Button(cInput.GetText(n, 2)) && Input.GetMouseButtonUp(0)) 
				{
				cInput.ChangeKey(n, 2);
				}
			}
			GUILayout.EndVertical();
			GUILayout.EndHorizontal();
			GUILayout.EndScrollView();
			GUILayout.BeginHorizontal();
			if (GUILayout.Button("Reset to Defaults") && Input.GetMouseButtonUp(0)) 
			{
				cInput.ResetInputs();
			}
			if (GUILayout.Button("Close") && Input.GetMouseButtonUp(0)) 
			{
			ShowMenu = false;
			}
			GUILayout.EndHorizontal();
		}
	}
}

3 Answers

3

Hi!
If you want it to be at the center you chould use Screen.width /2 ,Screen.height /2.
Or you can go at this by using percentages like this: Screen.width * 0.5f,Sreen.width * 0.5f.
Now it depends on how big is that you want to show because using what i just typed will make it start at that position not be centered so you will have to experiement with diferent values(mine for center text are usualy 0.40-0.43f.

already knew i could do this im just not sure how to with my script as i use loops

What you want to do is put GUILayout.BeginArea at the top of your OnGUI function, with an appropriate centred rect. This will move your whole GUI into the middle of the screen. You also need to end the area at the end.

The other answers have the appropriate rect defined for you.

Have you tried 4.6beta which with the new GUI system. I am having problem about UI as well.
Also you can do (screen.width/2 - width/2, screen.height/2 - height/, width, height)

im using 4.6 beta but not using the new gui i still find the old easier for some reason

check out their official tutorials. pretty useful. good luck

Lol. Your obviously not finding OnGUI easier if you can't centre your display.