I’m attempting to create a scrolling chat window ( using ScrollView ) and need to figure out how I can calculate the size of text inside a Label or TextArea. The text must be a fixed width, but can change in height. I will also need to know what the final size of the Label / textarea so I can build the ScrollView of multiple Label / textareas.
Thanks!
I think I can use System.Windows.Forms.Label to do this, however, I can’t seem to get Unity to recognize System.Windows.Forms although Mono does. How can I get rid of the following error inside Unity?
Assets/Main.cs(3,14): error CS0234: The type or namespace name Windows' does not exist in the namespace
System’. Are you missing an assembly reference?
You can not use System.windows in unity.
You can however use: GUIStyle.CalcSize (Unity - Scripting API: GUIStyle.CalcSize) to calculate the current size of a certain guistyle. If the guicontent will hold text or image, you could use it to calculate the runtime size.
You can also resize any GUI element using the Matrix4x4.SetTRS (Unity - Scripting API: Matrix4x4.SetTRS).
In your case you will probably want to scale the Y factor (since you say you wish to change the height). Using this on fonts will render a somewhat blurry font, since it makes the GPU scale what ever graphic data is given to it, which has no idea what a font is.
But unless you have a dynamic font which size’s only change in height (never recall I saw something like that), if you only want to change a font’s width, I think this is your only option.
Are you sure you can’t have the width change too? If so you could use dynamic fonts and use some sort of runtime font size picking depending on the amount of columns/rows you have room for. This will require some experiments on your part.
1 Like
Thanks for the info!
Using GUILayout.BeginScrollView and GUIStyle.CalcSize I bet I can make it work.