- Home /
Why can't GUI.Box have a width of 5 or less?
Hi, I am drawing a rectangle on the HUD to show the current energy of my character. When the energy is very low, my rectangle is not shown correctly. After some testing, I noticed that when the width of the rectangle is 5 or less, the rectangle just keep a width of 6 and is getting a negative x offset of the difference.
Try my code :
void OnGUI(){
DrawQuad(new Rect(12, 162, 2, 16), Color.green);
}
void DrawQuad(Rect position, Color color) {
Texture2D texture = new Texture2D(1, 1);
texture.SetPixel(0, 0, color);
texture.Apply();
GUI.skin.box.normal.background = texture;
GUI.Box(position, GUIContent.none);
}
It won't draw a rectangle to 12, 162 with a width of 2 like it should, instead it draws a rectangle to 8, 162 with a width of 6...
Why is that so?
Answer by Jamora · Jul 27, 2013 at 12:49 AM
This is because the default box style has a 6 pixel border to all sides. The GUIStyle page explains it well: "Border: Number of pixels on each side of the Background image that are not affected by the scale of the Control' shape"
ohhh, do you know if we can reduce this border or if there would be another way of creating a thin rectangle?
You can create your own GUIStyle and set all the values from code, or create a GUISkin, and modify all the values from the inspector. Don't forget to apply the skin in code... (I always do and wonder why aren't my styles working.)
Your answer
Follow this Question
Related Questions
full screen wide GUI.Box 3 Answers
Buttons in Rects, opening other Rects? 2 Answers
Problems with Rect 1 Answer
Rect.ymax wrong? 1 Answer
Is there a way to color in a rect? 1 Answer