Today testing the new GUI i was making a Counter Down, to make sure the text is at the correct size on every screen size i checked the “best fit” check box on the Text component.
I controlled the text by doing this:
time.text=""+ctrl.CountDown_timer+"";
I used “”+float+“” because the performance hit from float.ToString was huge in comparison (for some reason)
“time” is the Text Component
CountDown_timer is a float created like this(in another c#script):
CountDown_timer-=Time.deltatime;
the problem:
If i check “best fit” the game lags but the text is always the perfect size in every screen size
if i uncheck the game is completely smooth but the text size is fine in some screens and way too small on the others.
One of the problems with the legacy text system we are using is that it is kinda sucky. They only way to know if text fits within an area is to run a full generation pass (including glyph generation), see if it fits, and if it doesn’t try again with a different text size. So this is what we do. It has some super bad disadvantages, like being very slow and polluting the texture space with unneeded glyphs.
The error you are seeing is because the texture that is being generated for the font is literally surpassing the maximum allowed size in Unity.
We recommend using best fit sparingly and with a tightly configured minimum and maximum size.
If you are using this to support scaling to multiple devices one better way of doing this is to use the Reference Resolution component (which modifies canvas scale) and then use this scale to manually set the text size (the default size * scaling).
We will be addressing text rendering as soon as we can.
Tim, thank you so much for all of your candid and clear responses so far. This honest transparency is making it much easier to be successful with this new system moving forward. Thank you!
So it’s normal that best fit take one render frame to achieve ? I need to make a big size change in a component, and text need to best fit. When I change the size of the component (big to small), text stay appear bigger than background image during one frame, and this is very annoying on mobile device.
Will this behaviour change for release ? What do you advise to achieve this ?
I would take a look at TextMesh Pro which currently has a version (alpha) that works with uGUI. Take a look at this newly released video. The auto-sizing feature is shown around 12:00 minutes but I would still watch the whole video.
I spent hours hopelessly trying to figure out why my game had suddenly started to lag on a mobile device. Finally I got pro and found out the cause easily with the profiler.
In the HUD of my game, I had changed the maximum size for best fit from 30ish to 70. I get no penalty if the range of font sizes is under 30. Seriously - no spikes at all. Smooth as a baby’s bottom.
I’d have never thought to look into the HUD of my game, which had 6 text objects and very little text in each, though some being updated at every frame.
I spent about an entire day trying to pinpoint the problem. I was also having issues running my game smoothly on my mobile device. And I finally found that the problem was the ‘Best fit’. (I was starting to think it was some weird bug of my own)
I had 200 as the maximum size and the phone was freezing about two seconds each time I wanted to update a couple of texts.
I also have some other texts updating periodically, but the maximum size is not 200, it’s 40 and I have no problems with them.
Hi my friends,
I got this issues too, I migrated to new UI (from nGUI) …I have some question what range of Best Fit sizes do you use now that fixed your problems, and can be this laggy problem related to Canvas Scaler-Reference Resolution (in my case 1920x1080)…Thanks in advance !
I think this problem is still happening. What is the best option to deal with “Horizontal overflow”. A good settings could be: Best fit changes the font size base on resolution and the wrap method changes the scale of the text, so no additional texture recreating happens.