How do i get size in pixels of a GUI.Label for chat construction purpose ?


I am designing a chat system where i will write to the screen all the text that comes from net.

I need to be able to write lines of text with diferent colors, etc. I want to cut the entrance string and write in screen with GUI.Label with diferent fonts and/or colors. But i dont know how to know how many pixels will have the string in the destination label.

I know that in DX there is a function that makes a fake render of the text you want to write and returns you the destination size (width and height) of the rendered text before you render it.

With this, i will be able to cut long strings and make new lines.

Finally i will control how many lines user can view without GUILayout interaction.

So, the question is: Is there any function or methodology to get how many pixels a rendered string will size ?

I can't bypass it by count characters and have a maximum characters lenght, but 'i' cahracter doesnt have the same size as 'a' character.

Thanks for your time :)


Why don't you want to use GUILayout? If you do, you can get the size of an element using the GUILayoutUtility.GetRect method.

You can use the helper functions in GUIStyle to find the size of content

Functions you may want to look at:

If you're not using a specific GUIStyle, you can just use something like this (in c#)

GUIStyle style = "Label";

Which will assign the Label style from the currently used skin