Search Unity

TextMesh Pro Scaling Text properly for different resolutions.

Discussion in 'UGUI & TextMesh Pro' started by luke_Piazza, Mar 14, 2018.

  1. luke_Piazza

    luke_Piazza

    Joined:
    Mar 14, 2018
    Posts:
    3
    Hello! I've been trying to use TextMesh Pro for dialogue text and have been running into some issues. I've found that it's necessary to auto size text as a set font size will be too big or too small if the resolution of the game ever varies. My problem is that I would like to keep the text size at a certain ratio (somewhat small) but when I try to make the text box bigger to make space for multiple lines of text, the size of the text will scale to the size of the box. Is there a way to possibly set the size ratio of the text to a certain amount and not allow it to scale to the size of the text box with auto size enabled? Would greatly appreciate some help!
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    Text auto-sizing is designed to scale the text to the defined max point size to fit the text container or RectTransform in this case.

    Text Auto-sizing also needs to test various point sizes in order to determine what the optimum point size is for a given text container and as such is not as efficient as just a plain text object with a fixed size.

    Given the above, I always recommend users only use Text Auto-sizing to determine the optimum point size for a given text object and then disable this Auto-sizing feature and then manually set the size on all other text objects to this resulting optimum point size. This way you only get the impact of auto-sizing on one object while making sure the point size of all other objects is consistent with the first which does also better UI design / practices.

    So what comes to mind in terms of handling your scenario would be to use a single object whose size is driven by whatever scaling which is used with Auto-sizing but just for testing / determining this optimum point size. Then use this resulting point size on all other text objects. Since the point size on these other objects is manually set and static, you will be free to change the size of their text container to accommodate for multiple lines of texts.

    Let me know if you think this could work for your use case?
     
  3. luke_Piazza

    luke_Piazza

    Joined:
    Mar 14, 2018
    Posts:
    3
    First let me thank you for your quick response! I really appreciate it. From what I can tell is that you are recommending is that I use the Text Auto-sizing function to determine what point size would look best inside a certain object. My only problem with this is that a static point size will vary in size based on the resolution I run the game in. For example if I run my game in 4k quality at 72 point size, the text looks a reasonable size in my dialogue box, while if I run the game in 1080p with the font size still at 72 point, the font size will look way too large inside the same sized box. I apologize if I am misinterpreting what you are saying as I am very new to UI in Unity but would be grateful for a little clarification in what you're saying. Thanks again!
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    Correct. I am recommending that you use the Text Auto-Sizing to determine the optimum point size to use for any given resolution.

    Using your example, let's assume the text container that is used for determining the optimum point size when the resolution is 1080p with a text object set to auto-sizing returns a point size of 36 to fit the entire text in that container. Then on that same system (device) you would set the point size for all other text objects to 36. Now on a platform / device running at 4K, the same process would return a point size much larger since the text container would be larger due to having been scaled up. So in this case maybe the new point size is 72 which you would then use on all other text objects for this 4K resolution.

    Just to clarify a detail. If the default point size is set to 36 and Auto-sizing range is 18 to 72. Once the text object has been generated, checking the fontSize of the text object will reveal what the auto-sizing ended up using to fit the container.
     
  5. luke_Piazza

    luke_Piazza

    Joined:
    Mar 14, 2018
    Posts:
    3
    Thank you, really understand what you're saying now. Is there a font setting in the TextMesh Pro UGUI script that would allow you to set a certain font size (after you figure out the right size for each resolution) depending on what resolution the game is being run in? If not, is there a way you would recommend or given an example of how you would code that in? Thanks again for the detailed responses, really appreciate it.
     
  6. PabloIH

    PabloIH

    Joined:
    Sep 20, 2014
    Posts:
    12
    I had similar problems and if you want consistent text size using TextMeshProUGUI and AutoSize enabled. Just check that the Min and Max are enough to cover all resolutions and hodl that text into a Canvas with a Canvas Scaler -> Scale With Screen Size.

    Also if you are going to write down different size sentences in the same text box, and you want a consistent text size over all of them, you can find a "font size" just before filling the text as Stephan_B pointed out.

    I used this:

    Code (CSharp):
    1. void FindBestPointSizeForThisResolution() {
    2.         textMeshPro.enableAutoSizing = true;
    3.         textMeshPro.text = "Placeholder text to check the best point size for this resolution";
    4.         textMeshPro.enableAutoSizing = false;
    5.     }