Cant keep OnGUI() elements to the same position C#
As the title says, i cant get the elements inside OnGUI() to stay at the same position..i have tried to use GUI Matrix and many onther solutions but none of them actualy worked...this pretty much stops me from trying to develop any game, please help. ( i am mainly using c#)
Comment
Best Answer
Answer by Jessespike · Oct 30, 2015 at 06:37 PM
You can use Screen.height and Screen.width to get the size of the screen, which can be used to position elements. example:
void OnGUI()
{
float buttonWidth = 100f;
float buttonHeight = 40f;
Rect rect = new Rect(Screen.width / 2 - buttonWidth / 2,
Screen.height / 2 - buttonHeight / 2,
buttonWidth,
buttonHeight);
if (GUI.Button(rect, "click me")) {
Debug.Log("clicked");
}
}
Your answer
Follow this Question
Related Questions
How to make Text in canvas disappear on keypress? 2 Answers
Create GUIs with a lot of tables 0 Answers
I am doing a runner game, i need to detect a down swipe on the screen 0 Answers
GUI texture not showing up in unity 5? 1 Answer
Unity3d UI ListBox Help 0 Answers