Thanks, but my case is a bit more complicated, take a look at the screenshot:

This is a “balls hitting bricks” game, those are the bricks; each brick has a number, whenever it gets hit by a ball, the number goes down by -1.

The bricks may have different range of numbers, the brick that has “1000” should has smaller font than the brick that has “9”, because if they both have the same fontSize, the “9” brick would look too small or the “1000” brick would overflow.

The brick is a Prefab (SpriteRenderer gameObject and TMP gameObject), i’m using TextMeshPro component with MeshRenderer (no canvas).

If i follow your post, i have to run your script every time a brick gets hit (because the number changed) which is a LOT, and we’re talking about one cube only…

I thought a lot about how to make this possible, but i think that i can’t, i’ll end up doing exactly what “Auto Size” does!

So i guess i HAVE to use “Auto Size” in my case, right?

Thank you for providing this additional information and context.

I think you could still use the revised implementation I suggested in that post but instead pre-calculate the optimum point size per font asset using “0”, “00”, “000” and “0000” instead of words or lines of text.

In some loading / preparation process, you would pre-calculate the optimum point size for the given font asset using these “0”, “00”, “000” and “0000”. This would be done using auto-size only once.

Then while your game runs, you would be sure to set the pre-calculated point size on the relevant text objects (with auto-size disabled) based on whether the number contains 1, 2, 3 or 4 digits. This would ensure that all text for each of the digit group uses the same point size.

P.S. I picked zero since most of the time it is the widest digit but in this setup / pre-calculation phase, you could actually check the width of each digit for a given font asset to make sure you use the widest one in this calculation of the optimum point size.

@Stephan_B
Wow, i don’t know why I didn’t think of that. This is brilliant.

So calculate the optimal font sizes for 0, 00, 000, 0000 and save them in an array, then every time the cube gets hit, i check for the new number and apply the font size for the corresponding digit count. Right?