- Home /
GUILayout margin bug?
GUILayout seems to only draw margins correctly at some window sizes.
Steps to reproduce:
Create a GUI area, it can be any proportions but it must scale with screen width.
Inside the area begin a horizontal layout with 3 (arbitrary) GUILayout boxes inside.
Apply a skin which sets the box style's margins for left and right to be 1 pixel (also arbitrary it seems).
Resize the editor.
As the screen size changes the margin between the boxes will increase and decrease, ranging from what appears to be 3 pixels to a 1 pixel overlap. It even seems to happen if you set the margin to zero, sometimes there is a visible gap and sometimes the borders overlap each other.
This seems like a bug in Unity and it does persist in real builds.
public GUISkin skin;
void OnGUI() {
GUI.skin = skin;
GUILayout.BeginArea(new Rect(10, 10, Screen.width / 2.0f, 400));
GUILayout.BeginHorizontal();
GUILayout.Box("A");
GUILayout.Box("Column2");
GUILayout.Box("Column3");
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
These pictures show the same layout at two slightly different editor window sizes, resulting in neither of them showing the correct margins.
Is this a Unity bug or a mistake somewhere on my part?
Answer by DreadTalon · Sep 05, 2013 at 08:15 PM
What appears to be happening is that the layout manager takes the width of the container area, divides it into even portions for each child element and tries to fit the correct margin between the two. Sometimes the number of pixels in the container turn out to be "odd" ie it isn't possible to have the same pixel margin between each element AND have all the elements be the same size. In this case the layout manager seems to prioritize having the elements the same size and simply disobeys the margin to make it fit.
I think the way I got around this issue was to just set a hardcoded value for the container that is known to fit correctly. It might not be resolution independent. If you needed a resolution independent solution it might be possible to use a RoundToNearestMultiple function so that you get the closest scale to what you want that doesn't have an odd number of pixels that cause the margins to fail.
Answer by jorashwil · Mar 09, 2013 at 09:13 PM
You have 2 options to my knowledge, either 1. use a sprite or 2. define a size for each box. The overlap is normal or residual is normal. It's like if you make a health bar and set it as: GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth, myStyle)
you'll still be left with a dark line when it hits 0. This is because of the division of curHealth and maxHealth. So 0/100, returns 0, but the box doesn't disappear because they're is still a value of 0 out of 100.
I'm not sure how it can be considered to be working as intended. If you set the margin to zero, sometimes there is a multiple pixel gap between the boxes. Isn't the correct behaviour to always have the margins obeyed and have the boxes sized appropriately?
Your dividing 1 big box not setting 3 boxes, try making 3 boxes ins$$anonymous$$d.
I found the same bug and it still persists It is some Unity problem and it can't be fixed I think