Would anyone out there be willing to share their methods for automatically wrapping text? Scripts, techniques, etc. I’d like to make something of the sort, but am timid about getting started on the wrong foot.
Talzor posted some code that works for monospaced fonts in another post
Cool. I have another question: how does the BoxCollider figure out its default size? When enclosing 3D text in a BoxCollider, I could get the BoxCollider length as an indication of line length — it would be ideal if I didn’t have to replace the box collider after every word as in Talzor’s script, though.
I’d guess you are looking for renderer.bounds:
http://unity3d.com/Documentation/ScriptReference/Bounds.html
-Jon
That’s it!
EDIT: Is there a way for me to reset the bounds (reencapsulate)? I’m adding to a string of 3D text and want the bounds to reset every time TextMesh.text is set. This happens whenever I set the text in the inspector, but I want to trigger it with scripting.
At the moment the textmesh bounding volume generation is delayed until just before rendering. So if you set the text, then reading back the bounding volume from a script will give you back the new bounds with a one frame delay.
revive :twisted:
Hooray and thanks everyone!
Based on some wrapping code someone made for the GUIText and thanks to the hint for the bounds here and thanks to the bounds delay thing being fixed, I managed to produce the following code that I’d like to share with you:
public float lineLength = 7;
public void SetText(TextMesh textMesh, string text)
{
if (string.IsNullOrEmpty(text))
{
textMesh.text = "";
return;
}
string[] words = text.Split(" "[0]);
string result = "";
for (int index = 0; index < words.Length; index++)
{
string word = words[index].Trim();
if (index == 0)
{
result = words[0];
textMesh.text = result;
}
if (index > 0)
{
result += " " + word;
textMesh.text = result;
}
float textSize = textMesh.renderer.bounds.size.x;
if (textSize > lineLength)
{
//remover
result = result.Substring(0, result.Length - (word.Length));
result += "\n" + word;
textMesh.text = result;
}
}
}
Is this really fixed? This doesn’t work for me, the bounds are not updated after the setting of the text (textMesh.renderer.bounds.size.x remains zero).