I am trying to center a GUILayout.Label but I guess I am not doing it right because it will not work:
GUILayout.BeginHorizontal();
GUILayout.Label(message, new GUILayoutOption[] { new GUILayoutOption(GUILayoutOption.Type.alignMiddle, true) });
GUILayout.EndHorizontal();
AngryAnt’s version is what I use, too. The GUILayoutOptions are for text within the element itself (so the alignment of the text inside the label’s bounding box) rather than the positioning of the element in the window or container.
Once you get the hang of using FlexibleSpace, you have have very clean, attractive layouts without a lot of work.
Really? That has never worked for me. I just tried putting what Emil posted in an OnGUI function with the default skin and the text is sitting up in the upper left corner of the screen instead of the upper center.
We use the explicitly positioned GUI stuff I guess for performance and developer friendlyness.
Interesting. I have no idea what would cause that except
Edit:
Funky… It seems that unity needs an active area defined - that it doesn’t default to the entire screen space when nothing else is specified. The following code illustrates how it works when an area is defined:
Jon,
Is that performance difference something you’ve measured? What do you see as the performance hit for using GUILayout vs absolute?
I’m curious because we’ve used GUILayout almost exclusively – I found it a lot more friendly to code once I got the hang of it, but I’d love to know if there are critical places I shouldn’t use it.
Well, the best example I have is the RC Laser Warrior track selector. The initial version had that using GUILayout stuff. When the track selector is loaded the window fades and pops in using 0.3 second GUI.matrix animation. With the GUILayout version, you wouldn’t really see that animation because there was a stutter when the window was being loaded. By moving to the straight GUI stuff it’s now completely smooth. There is admittedly a fair amount going on in that window.
It was between that type of thing and the layout changing between versions and I just stopped using it. I totally understand it’s nice to get something up and running quickly if your layout isn’t too crazy. But GUILayout has a performance tradeoff for its benefits. It was important enough for the iPhone version to be able to turn off GUI layouting.
Ahah! Now I guess if I’d bug report this and it’d get fixed/changed, it might screw up people’s published GUILayout using games. (That’s another reason we don’t use it, it’s not worth the risk and testing time for me.)