- Home /
How do i get size in pixels of a GUI.Label for chat construction purpose ?
Hi
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 :)
LLORENS
Answer by Mike 3 · Jun 03, 2010 at 05:44 PM
You can use the helper functions in GUIStyle to find the size of content
http://unity3d.com/support/documentation/ScriptReference/GUIStyle.html
Functions you may want to look at: http://unity3d.com/support/documentation/ScriptReference/GUIStyle.CalcSize.html http://unity3d.com/support/documentation/ScriptReference/GUIStyle.CalcScreenSize.html http://unity3d.com/support/documentation/ScriptReference/GUIStyle.CalcHeight.html http://unity3d.com/support/documentation/ScriptReference/GUIStyle.CalcMinMaxWidth.html
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
Answer by jonas-echterhoff · Feb 11, 2010 at 12:35 PM
Why don't you want to use GUILayout? If you do, you can get the size of an element using the GUILayoutUtility.GetRect method.