- Home /
How to measure the width of a string?
I'm using text mesh pro but need to format in ways that are not supported, like keeping the first and last name on the same line and using a custom overflow indicator. I wrote a script that did this by keeping track of how many characters are on the line to see if the word or pair of names will fit but it is not accurate enough. I'm looking for a way to measure the width of the word so that it takes into account the variation in character widths and the difference between lower/upper case.
Wait are you talking about the number of characters in a string? Or the actual width in pixels for its on screen representation? Because the second one depends on a whole lot of factors.
Well for this you will need to know first the measure of your text mesh object in Unity's world which you can get using Bounds/Extents. An important distinction in this problem is orthographic or perspective camera? if it's perspective then your field of view will warp objects in screen as well as the distance between your text mesh and your camera.
Your answer
Follow this Question
Related Questions
if statement not working for user input 0 Answers
"Animate"/look handwritten font on UI elements 1 Answer
Quick Question on UI Text Alignment 1 Answer
Positions of UI Text characters. 0 Answers