0

How do you auto-size text on a textmesh.

I have dice, with six sides, that I want to put words on. The words will constantly be changing, so I need them to auto size based on the size of the cube, which doesn't change.

The dice are not UI elements and thus sit outside the canvas.

Here is how I am adding the text.

 LocationSide1.GetComponent<TextMesh>().text = "1"; LocationSide2.GetComponent<TextMesh>().text = "2"; LocationSide3.GetComponent<TextMesh>().text = "3"; LocationSide4.GetComponent<TextMesh>().text = "4"; LocationSide5.GetComponent<TextMesh>().text = "5"; LocationSide6.GetComponent<TextMesh>().text = "6"; 

Imagine I want the dice to say "pickle" instead of the 6. The next role it might say "cantaloupe" then "pea". The text will change sizes.

Example how the text sticks over the edge.

enter image description here

3
  • 1
    Tim, if you could remove your requirement to use a TextMesh, you can render UGUI Text components directly into world space and then use their BestFit option. docs.unity3d.com/Manual/HOWTO-UIWorldSpace.html. I know this isn't an answer to your question, just another possible option in case you hadn't considered it. Commented Nov 24, 2016 at 0:07
  • 1
    Yeah, I agree... Text Component is imho also a better option. Commented Nov 24, 2016 at 0:10
  • thanks I will try that. Commented Nov 25, 2016 at 17:28

2 Answers 2

0

The answer you seek can be found on another thread - http://answers.unity3d.com/questions/218222/how-to-calculate-a-textmesh-width-without-renderin.html

 public class TextBehaviour : MonoBehaviour { public TextMesh textmesh; void Start () { Bounds bound = BoundOfTextMesh("New Text"); print ("Size X = "+bound.size.x +"Size Y = "+bound.size.y);// Size of X and Y or Vector2 } Bounds BoundOfTextMesh(string textMeshName) { textmesh = GameObject.Find (textMeshName).GetComponent<TextMesh>(); return textmesh.renderer.bounds ; } } 
Sign up to request clarification or add additional context in comments.

5 Comments

hmm I am not smart enough to understand how that answers my question :-(
Well, its really a tricky approach. If you were using a "text" component I'd point you to Text.preferredWidth to get your calculated answer, but you've gone deeper beyond that wrapper so you need to follow alongside its way of calculating that property.
is there a way to put the text on the side of a dice?
I've added some code that may help point you in the direction you need.
@Scott, the question was how to size the text to fit to a fixed-size mesh; not how to size the mesh to the text.
0

TextMesh doesn't have an auto-sizing feature, so to resize the text to fit in its container, you would have to follow Scott Barnes' suggestion of manipulating the Renderer for the desired effect.

If you're looking for alternatives that have the effect you're looking for, you might consider TextMesh Pro. It's a free asset on the Unity store that has Auto-Sizing functionality. The text comes in both 2D UI and 3D Text formats, so it will work both inside and outside of canvases.

TextMesh Pro Auto-Sizing Video

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.