- Home /
Centering a GUI label with varying text length
There is an layout area that contains a label. The label has a dynamic text, which can change in length. Therefore the effective height and width of the area is not know. The label is mark as no wrap in the gui skin. How can I make this layout area appear in the middle of the screen?
public Texture2D image
private int step = 0;
private String[] stepMessages = { "one", "two", "very long text", "even longer text with \n breaks........" };
void OnGUI()
{
// This whole area should appear in the middle of the screen
GUILayout.BeginArea(new Rect(???, ???, ???, ???));
GUILayout.BeginVertical("box");
if (step > 0) {
GUILayout.Label(image);
}
GUILayout.Label(stepMessages[step]);
GUILayout.EndVertical();
GUILayout.EndArea();
}
Answer by BerggreenDK · Aug 13, 2011 at 10:05 PM
I did that by defining my own GUIStyle and using that on the element as you can defined the alignment inside a GUIStyle.
http://unity3d.com/support/documentation/ScriptReference/GUIStyle-alignment.html
http://unity3d.com/support/documentation/ScriptReference/TextAnchor.html
I am not sure how to do it. Isn't text anchor control alignment of text inside the label? How do I define my own GUIStryle and what is the difference between GUIStyle and GUIS$$anonymous$$in? Can you provide an example?