2

So, I am trying to use Unity2D to create a TCG/CCG video game, and I seem to have hit a light bump: I cannot seem to efficiently resize horizontally text that ends up being larger than its container. (Example: Text has a width of 200, I want to fit it in 170). I mean, Unity did have a "fixing" with the option "Best Fit", but it didn't quite work as well as it should have, instead changing the font size to better fit it (Which is not the expected result.)

Now, I know that I could just find the width of a text, check if it overflows, and then apply simple math to scale it on the width side. That's something I'm currently trying to put into the code: But! I am looking for something I would have overlooked within Unity. Something that would do it faster, without me having to hard code it.

Now, what I have tried thus far (to avoid repeat of those offers):

  • Best Fit (actually changed the font size, which didn't fit my needs)
  • Both "Horizontal Overflow" settings
  • Content Size Fitter (Additionally, I seem to not be able to GetComponent<> that one.)

If you know of any trick I should probably know, feel free to let me know: It'd help my coding a lot.

Thanks in advance!

  • I don't know of a tool that will automatically shrink your text in unity, but you can use: https://docs.unity3d.com/ScriptReference/TextGenerator.GetPreferredWidth.html to calculate the size of your font, and adjust the font size until you find one that is <= to your current size. – AresCaelum Mar 22 '18 at 13:13
  • Thanks a bunch! Quite a few steps less than expected, so I'll actually take it! – Guillaume Lafrance Mar 22 '18 at 15:07

1 Answers1

3

TextMesh Pro is a free Unity Asset that has support for Auto-Sizing and fitting text in specified areas, both as text floating in 3D Space, and as UI Text. Upon creation of a TextMesh Pro object, there is a setting called "Auto-Size" which is what you're looking for.

Details on how Auto-Size works can be found here.