- Home /
Scaling the space between GUI elements
I have a stack of buttons aligned together in a menu, but unfortunately they don't appear the same at different resolutions. Normally, there is between 1/2-1/4 centimetre of space between each button, but at <1920*1080 they grow closer and begin to overlap.
I've tried to use FlexibleSpace and padding, but sadly neither of them have worked.
Code sample:
#pragma strict
var customButton : GUIStyle;
var customButton2 : GUIStyle;
var customButton3 : GUIStyle;
var customButton4 : GUIStyle;
//private var rctOff : RectOffset;
function Start(){
}
function OnGUI () {
if(GUI.Button (Rect(Screen.width*0.015625, Screen.height*0.014286, 75, 25), "Sandbox", customButton)){
Application.LoadLevel("gameDemo");
};
/*GUILayout.BeginArea(Rect(Screen.width*0.015625, Screen.height*0.013, 75, 20));
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUILayout.EndArea();*/
if(GUI.Button (Rect(Screen.width*0.015625, Screen.height*0.052631, 75, 25), "Multiplayer", customButton2)){
Application.LoadLevel("");
};
//GUILayout.FlexibleSpace();
if(GUI.Button (Rect(Screen.width*0.015625, Screen.height*0.09091, 75, 25), "Options", customButton3)){
//Application.LoadLevel("");
};
//GUILayout.FlexibleSpace();
if(GUI.Button (Rect(Screen.width*0.015625, Screen.height*0.12987, 75, 25), "Exit", customButton4)){
System.Diagnostics.Process.GetCurrentProcess().Kill();
};
GUI.skin.button.padding.top=20;
}
Pictures:
Answer by YoungDeveloper · Oct 07, 2013 at 06:25 AM
One thing you could do, is not calculate button size from Screen.width and height, but instead use constant numbers.
I'm not, though. The screen width/height things are positional.
Oh, yea i noticed, but it's still the case, because the buttons base position becomes so small that it overlaps one another. Try this, didn't test it though..
function OnGUI () {
float a = Screen.height*0.014286;
if(GUI.Button (Rect(Screen.width*0.015625, a, 75, 25), "Sandbox", customButton)){
Application.LoadLevel("gameDemo");
};
if(GUI.Button (Rect(Screen.width*0.015625, a+25, 75, 25), "$$anonymous$$ultiplayer", customButton2)){
Application.LoadLevel("");
};
//GUILayout.FlexibleSpace();
if(GUI.Button (Rect(Screen.width*0.015625, a+50, 75, 25), "Options", customButton3)){
//Application.LoadLevel("");
};
//GUILayout.FlexibleSpace();
if(GUI.Button (Rect(Screen.width*0.015625, a+75, 75, 25), "Exit", customButton4)){
System.Diagnostics.Process.GetCurrentProcess().$$anonymous$$ill();
};
GUI.skin.button.padding.top=20;
}
Unity is telling me to add a semicolon after "float a = Screen.height*0.014286;". How should I fix this?
Answer by Jamora · Oct 07, 2013 at 09:09 AM
A GUI.SelectionGrid should keep the spaces constant. You'll have to play around with GUIStyles to get the spacing between them correct, but once that's set it shouldn't change when the resolution changes.
Your answer
Follow this Question
Related Questions
loop GUI Buttons doesn`t respond 0 Answers
Is this the correct way to scale my GUI? 1 Answer
GUI Resolution Ajust 1 Answer