- Home /
GUILayout.Space() doesn't work with floats?
I think this must be a bug. It's the kind of thing that makes me pull my hair out. In the example below, whoRect and tmpRect are both returned from GUILayoutUtility.GetLastRect(). My goal is to align text to columns based on the width of the column header such that text + space = width. I had planned on doing this by calling GUILayout.Space() but it doesn't work with the results for GetLastRect() for some extremely odd and strange reason I cannot for the life of me fathom.
// simplest test (this works)
float x = 78f;
float y = 23f;
int diff = (int)(x - y);
GUILayout.Space(diff);
// this doesn't
x = whoRect.width;
y = tmpRect.width;
print("x " + x + " y " + y); // 78 and 23
diff = (int)(x - y); // note: also tried with diff as a float
print(diff); // 55
GUILayout.Space(diff);
So when I use the difference from the two Rects I don't get any space. The value prints out identically in the two calls to GUILayout.Space() so why does the first work and the latter not?
goes off and bangs head against wall some more
I find it hard to believe it could be sequencing when the print(value) call is immediately prior to the Space(value) call and prints out the correct value. And yes, I switched from floats to ints to get rid of any floating point value weirdness. Now, I am a C++ programmer so I do make some C# assumptions that aren't always valid. But I thought Rect saved = LastRect() would COPY the last rect request into the saved value. Sequencing at that point should be irrelevant so I'm guessing something weird is going on behind the scenes and I don't have the source to look at. Here is the gist of the code I was using.
doGUI()
{
window = GUI.Window(1, window, drawWin, "myWindow");
}
drawWin()
{
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Button("column1", "myButton");
Rect saved1 = GUILayoutUtility.GetLastRect();
GUILayout.Button("column2", "myButton");
Rect saved2 = GUILayoutUtility.GetLastRect();
<... more of same...>
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("some text", "myLabel");
Rect temp = GUILayoutUtility.GetLastRect();
float diff = saved1.width - temp.width;
GUILayout.Space(diff);
GUILayout.Label("more text", "myLabel");
temp = GUILayoutUtilility.GetLastRect();
diff = saved2.width - temp.width;
GUILayout.Space(diff);
<... more of the same ...>
GUILayer.EndHorizontal();
GUILayer.EndVertical();
}
So what could be going on in the GUILayout that would mess with these values and make them irrelevant even after they printed out correctly? I'm thinking someone with access to the source code and some assembler skills could figure this out. I don't have the first and am limited in the second, so I'm just passing this along as a bug. If not a bug, but a feature, could you document it please so I know how to do what I want to do?
Answer by Paulo-Henrique025 · Aug 18, 2012 at 01:24 AM
Let's take a look at the documentation:
static function Space (pixels : float) : void
Pixels, the amount of space is measured in pixels and there is no thing as 1.5 pixel. It's easy to miss this kind of stuff.
That's actually irrelevant. Unity works with floats and all rendering is also done with floats. Of course it get rasterized in the end, but that should be obvious. He actually cast it already to "int" so he has whole numbers.
Answer by Bunny83 · Aug 18, 2012 at 02:26 AM
I'm pretty sure that's more sequential problem. Where and when do you use GetLastRect? GetLastRect only returns in the Repaint event a meaningful value.during the layout step it's garbage since the size of each rect can only determined at the end of the layout step.
So within the same OnGUI you can't use a rect value within the layouting phase. We don't know the rest of your script so we can't say what you actually want to do...
Your answer
![](https://koobas.hobune.stream/wayback/20220613075825im_/https://answers.unity.com/themes/thub/images/avi.jpg)