Change gui box height based on text content?

Hi All,

I have variable length text files. My requirement is the GUI box length/height should change based on the text content such that the text content should fit within the box.
And also the text content in the box should get aligned.
Suggest any ideas.

Thanks in advance.

If the width of the box is known, then you can use GUIStyle.CalcHeight. If you want to calculate width as well, then you have to use GUIStyle.CalcMinMaxWidth first. Please note that the second method will return different width values only if your style has word wrapping enabled. Text alignment can be set in your GUI style in the inspector, or programmatically by GUIStyle.alignment.

Word of warning: avoid modifying default style, like

GUI.skin.box.alignment = TextAnchor.MiddleCenter;

because this is not reversible (or at least not straightforward). If you want to play with style properties, just create a new style and configure it in the inspector. Or alternatively, create new one in code using default style as constructor parameter:

var myStyle = new GUIStyle(GUI.skin.box);