- Home /
Cannot get 3D Text/Text Mesh to wrap or a attached collider used as a button to scale based on word count.
I'm using 3D Text/Text Mesh in Unity3D for dynamically loading text, but cannot get the text to wrap or a collider attached to the 3D Text/Text Mesh to scale based on growing word count.
Hi the text format works fine if the word length is kept $$anonymous$$imum. If we have word length greater and we keep on typing with out space it goes out of bounding box. please check typing with out leaving space. Help me hoe to break that and bring that it in to bounding box.
Answer by ellens · Jul 05, 2012 at 02:51 PM
I had the same problem. With textMesh.renderer.bounds.size I used the following method to create line breaks:
function formatText(textToFormat : String, textObj : TextMesh , desiredWithOfMesh : float) : String {
var words = textToFormat.Split(" "[0]);
var newString = "";
var testString = "";
for (var i = 0; i < words.length; i++){
testString = testString + words[i] + " ";
textObj.text = testString;
var textSize = textObj.renderer.bounds.size.x;
if(textSize > desiredWithOfMesh) {
testString = words[i] + " ";
newString = newString + "\n" + words[i] + " ";
} else {
newString = newString + words[i] + " ";
}
}
return newString;
}
Call the method with:
var myText = formatText(myTextString, myTextMeshObj, maxWidth)
This code is awesome! Thank you ellens!
I made a $$anonymous$$or adjustment to make it work for text$$anonymous$$eshes that are rotated or parented to other objects. This should work for any text$$anonymous$$esh, ever:
function formatText( textToFormat : String, textObj : Text$$anonymous$$esh , desiredWidthOf$$anonymous$$esh : float ) : String
{
var words = textToFormat.Split(" "[0]);
var newString = "";
var testString = "";
for (var i = 0; i < words.length; i++)
{
testString = testString + words[i] + " ";
textObj.text = testString;
var textRot = textObj.transform.rotation;
textObj.transform.rotation = Quaternion.identity;
var textSize = textObj.renderer.bounds.size.x;
textObj.transform.rotation = textRot;
if(textSize > desiredWidthOf$$anonymous$$esh)
{
testString = words[i] + " ";
newString = newString + "\n" + words[i] + " ";
}
else
newString = newString + words[i] + " ";
}
return newString;
}
Also here is a slightly modified version, which I developed for my own purposes but may come in handy for others. With this, set your Text$$anonymous$$esh.text to whatever you want. Then, just call this function with the Text$$anonymous$$esh and the desired width:
static function FormatText( textObj : Text$$anonymous$$esh , desiredWidthOf$$anonymous$$esh : float )
{
var words = textObj.text.Split(" "[0]);
var newString = "";
var testString = "";
for (var i = 0; i < words.length; i++)
{
testString = testString + words[i] + " ";
textObj.text = testString;
var textRot = textObj.transform.rotation;
textObj.transform.rotation = Quaternion.identity;
var textSize = textObj.renderer.bounds.size.x;
textObj.transform.rotation = textRot;
if(textSize > desiredWidthOf$$anonymous$$esh)
{
testString = words[i] + " ";
newString = newString + "\n" + words[i] + " ";
}
else
newString = newString + words[i] + " ";
}
textObj.text = newString;
}
Hi the text format works fine if the word length is kept $$anonymous$$imum. If we have word length greater and we keep on typing with out space it goes out of bounding box. please check typing with out leaving space. Help me how to break that and bring that it in to bounding box.
Answer by alienheretic 1 · Mar 23, 2011 at 11:04 PM
try this it works for me make sure you add a box collider to the text mesh
var text_mesh=transform.GetComponent(TextMesh);
collider.size.x= renderer.bounds.size.x * 4;
collider.size.y= renderer.bounds.size.y * 3;
collider.center=Vector3(-1.0,0.5,0.0);