I’m using GUIText for all our on screen text, and I was wondering a few things:
-
Is there any way to make the text wrap at a certain width?
-
Is there any way to find the width and height of the area used by the text.
-
Is there any way to access the mesh of the text? (It creates a quad for each letter, right?)
Generally, is any way to find out anything about the physical layout of the characters on screen.
Thanks in advance.
GUIText has a GetScreenRect method that returns the size of the box onscreen. I don’t think you can set it to wrap or get a mesh.
I’m not sure where I found this, but this is a slightly modified version of a wordwrapper I found somewhere.
I thought I found it on the unify wiki, but I was unable to find it… So I dug it up for you 
var lineLength = 400; // Maximum width in pixels before it'll wrap
private var words : String[];
private var wordsList : ArrayList;
private var result = "";
private var TextSize: Rect;
private var numberOfLines = 1;
function cleanUp() // Easy to call from an external button
{
FormatString(guiText.text);
}
function FormatString ( text : String ) {
words = text.Split(" "[0]); //Split the string into seperate words
result = "";
for( var index = 0; index < words.length; index++) {
var word = words[index].Trim();
if (index == 0) {
result = words[0];
guiText.text = result;
}
if (index > 0 ) {
result += " " + word;
guiText.text = result;
}
TextSize = guiText.GetScreenRect();
if (TextSize.width > lineLength)
{
//remover
result = result.Substring(0,result.Length-(word.Length));
result += "\n" + word;
numberOfLines += 1;
guiText.text = result;
}
}
}
Hope that helps 
1 Like
Urgh, can’t believe I didn’t see that. I knew it existed for GUITexture, but for some reason it didn’t cross my mind that it would exist for GUIText as well.
Thanks for the code JTBentley, although it looks like a bit of a Schlemiel the painter’s algorithm to me 
Well, like I said, its not actually my code 
Feel free to improve upon it and post it here 
Well yeah, it gets the job done, but I’ll probably end up rolling my own as I need to handle hyphens and other things anyway.
A place to start is better than nothing 
Hi JT,
I came across your code snippet here and used it as a base for a simple guitext formatter. In case somebody stumbles across this thread here’s my C# version, reasonably optimized.
public static Rect FormatGuiTextArea(GUIText guiText, float maxAreaWidth)
{
string[] words = guiText.text.Split(' ');
string result = "";
Rect textArea = new Rect();
for(int i = 0; i < words.Length; i++)
{
// set the gui text to the current string including new word
guiText.text = (result + words[i] + " ");
// measure it
textArea = guiText.GetScreenRect();
// if it didn't fit, put word onto next line, otherwise keep it
if(textArea.width > maxAreaWidth)
{
result += ("\n" + words[i] + " ");
}
else
{
result = guiText.text;
}
}
return textArea;
}
1 Like
This was exactly what I needed; thank you for the post. 
Super helpful, even this many years later. Thank you!
Many thanks, I was programming a script to this but now I see that there are one working 