Script-class MyGUILabel
This class has two private values: the text to be displayed, and a bool, defining whether or not to fully center the text. Its OnGUI() method will add a GUILayout.Lable() to the GUI, and if the bool is true, it will add other GUILayout components before and after the label to ensure the label is both vertically and horizontally centered when displayed.
This is an example of how some of the complexity of an interface can be delegated to its own class like this. By setting a single bool to true, several Begin/End/Vertical/Horizontal statements and FlexibleSpace statements are added to the GUI that is output.
The screenshot illustrates how the flexible space and vertical/horizonal groups result in the desired alignment ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access