Search Unity

[NEW GUI]Text "bestfit" Performance Hit

Discussion in 'UGUI & TextMesh Pro' started by diogomota, Aug 21, 2014.

  1. diogomota

    diogomota

    Joined:
    Jul 6, 2012
    Posts:
    7
    Hi,

    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:

    Code (CSharp):
    1. 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):

    Code (CSharp):
    1. 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.

    Btw: while using Best fit there was an error:
    to solve this i used:
    Code (CSharp):
    1. [code=CSharp]time.text=ctrl.CountDown_timer.toString("F2");
    but no luck...

    Any suggestions?
     
    YusukeAoyama likes this.
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    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.
     
    wikmanyo, pea, mh114 and 2 others like this.
  3. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    902
    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!
     
    Visout, Marble and StarManta like this.
  4. diogomota

    diogomota

    Joined:
    Jul 6, 2012
    Posts:
    7
    Thank you Tim!

    Used the Reference Resolution component and it worked flawlessly
     
    shkar-noori likes this.
  5. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    178
    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 ?
     
  6. Alex Storm

    Alex Storm

    Joined:
    Jul 21, 2014
    Posts:
    11
    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.
     
  7. VBsec

    VBsec

    Joined:
    Aug 1, 2014
    Posts:
    7
    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.
     
  8. DominatorDuck

    DominatorDuck

    Joined:
    Jan 8, 2013
    Posts:
    3
    Thanks for that info!

    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.
     
  9. RoyalCoder

    RoyalCoder

    Joined:
    Oct 4, 2013
    Posts:
    301
    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 !
     
  10. Bravo_cr

    Bravo_cr

    Joined:
    Jul 19, 2012
    Posts:
    148
    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.
     
  11. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    we have someone working on this atm. not a full TextGenerator rebuild but at least smarter bestfit generation.
     
    feiting likes this.
  12. Bravo_cr

    Bravo_cr

    Joined:
    Jul 19, 2012
    Posts:
    148
    Thanks phil, great to know :)
     
  13. dazzle_wolf

    dazzle_wolf

    Joined:
    Sep 24, 2015
    Posts:
    2
    Hello!

    I'm still encountering this issue, what is the status of this?
     
  14. Ivalaostia

    Ivalaostia

    Joined:
    Feb 16, 2013
    Posts:
    2
    I'm also STILL encountering this issue with Unity 2020.3.3f1, are you even thinking to resolve this?
     
  15. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    TMP was acquired in 2018 its recommended you use it over the native Text system.