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 

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();
}

1 Answer

1

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 GUISKin? Can you provide an example?