[Bug] Textmesh Pro Doesnot work correctly with ugui layout group and layout element

Hi, I am using the newest unity 2019.3 but when I create layout group texmesh pro rect transform is way bigger as it should see image below. Basically when I am changing the size of text it is also changing the size of rect transform which is wrong

Correct with old Text


Wrong With TextMesh Pro (auto size is turned on)

Also text mesh pro is not respecting layout element, I was trying to set up flexible width but its doing nothing,

Are you using the TextMeshProUGUI component located in UI - Text - TextMeshPro?

If you are, can you provide a simple reproduction scene of your setup for me to look at?

Yes I am using Textmesh pro UGUI
I will make you scene in 2 days. I dont have PC right now with me.

Hi, I pack 2 scenes representing the problem. When you open the scene A and you will make the canvas with textmesh pro active. You will need to import text mesh pro essentials. But after importing it, you have to reopen the scene again. For some reason, the textmesh pro will not be updated correctly, if you don’t open the scene again, not sure why.

5162306–511880–Scenes.zip (9.38 KB)

Thank you for providing the test scenes.

I reviewed the provided scenes and the layout differences are due to the following:

  • UI Text component is using Arial whereas the TMP text component LiberationSans. Although these two fonts are very similar in terms of metrics, you should be using the same font when comparing these results.
  • The auto size / best fit ranges are different. The UI Text component has a maximum range limit of 300 whereas the TMP Text component does not. The max range for UI Text is set at 300 whereas the TMP Text component is set to 1000. Again when comparing, you should be using identical ranges.
  • The UI Text does not have kerning whereas the TMP Text component has kerning enabled.

Using the same font and auto size settings and disabling kerning on the TMP Text component, we get the following result.

As you can see above, the layout is virtually identical.

Note that Kerning should remain enabled on the TMP Text component (found in the Extra Settings) as it does produce better results which in the above case is adjusting the spacing between T and e which is too wide without kerning.

In terms of flexible width, attached is a simple scene where this appears to be working as expected.

5164628–512297–Layout Component Test Scene.zip (4.16 KB)

The problem has nothing to do with font difference, the problem is that text size is resizing recttransform which is wrong. Rect size should be fixed and the text should adjust to width and height of that rect, not to resize it.
I dont understang how you cant see the problem

1 Like

Using the same settings on UI Text and TMP Text with the same font (using Roboto Regular) with Auto Size disabled produces the same results (as far as I can tell) with any point size. See image below of the two using the Scene B you provided using point size of 200.

The super subtle grey outline around the text is UI Text.

Changing the point size of UI Text or TMP Text will affect the Preferred Width of those text objects which will affect their RectTransform width. Below is an image of UI Text and TMP Text using 150, 275 point size where you can see how this point size affects the Preferred Width and RectTransform width.

As you can see the results are virtually the same which produces the same layout results. The RectTransform width is driven by the Preferred values in both cases.

Now when using Auto Size / Best Fit, we do have a key difference where the Preferred Width reported by UI Text will be based on the point size of the text object as opposed to the resulting point size of the best fit.

By contrast, the Preferred Width reported by TMP will be the optimum / resulting point size of Auto Size.

In the above image, both text objects have a point size of 0 with Auto Size / Best Fit range of 0 to 300. In both cases, the resulting Auto Size / Best Fit ended up being 300 points. However, in the case of UI Text, the reported Preferred Width is 66 which is no where near the actual width of the text at this 300 point size. This is incorrect in my opinion. By contrast, TMP text object reported Preferred Width is 1257.19 which is exactly the width of the text object at 300 point size.

Back to your reproduction scenes, since your UI Text objects have their point size set to zero which returns a very small and incorrect preferred width, this makes it seem like the RectTransform size is not affected by the size of the text.

With regards to UI Text and TMP returning different results / Preferred values when using Auto Size, we can certainly discuss that.

Preferred Width currently represents what the width of the text would be if the width of the text container was infinity and when using Auto Size, this means the width of the text at the maximum of the range. In other words, no restrictions on the text width / the Preferred Width.

When you are building Ui you are defining the rect sizes up front, and you just want the text to fit inside the rect transform especialy with autosize on, you dont want text to change rect transform. It doesnot make sense.
I want to make ui which works on lot of different screen resolution and my rect transforms are respecting that. What is breaking it is that the text mesh pro text is changing the rect width without respecting the size of the rect. The old text is working correctly

Spoke with the UGUI team this morning and this is in fact a bug with UI Text as it does not return the correct Preferred Width. For instance, with Best Fit enabled, if the point size of UI Text is 0, the preferred width is 66. If you change this point size to 1 then the preferred width is 6.

Like I said before, if you disable Best Fit on UI Text and change the point size to any value from 0 to 300 you will observe the RectTransform and Preferred Width being accurately reported.

Having said that, I understand the resulting behavior of this UI Text bug is desirable for your use case. Since this bug results in returning a very small preferred width, you can override the Preferred Width using the Layout Element you already have on the TMP text object.

For instance, if you were to set the Preferred Width to 0, the width of the RectTransform of the text would be whatever the Horizontal Layout group comes up with since the text Preferred Width is small than that value. Ie. the RectTransform is increased in size only when the Preferred Values requires it. If you force a smaller Preferred width then it will always be driven by the Horizontal group.

5168102--512771--upload_2019-11-12_12-27-8.png

In your setup, each Child panel have a preferred width of 10. If the text was given a preferred with of 10 in the Layout Element, they would all be the exact same size.

I understand what you saying, but I don’t want to use the absolute value of preferred width because my rects are in relative size. I would rather use Flexible width, which is a relative value. When the screen size changing and you want to develop a multi-resolution game you don’t want to put absolute values

But if the rect width is driven by horizontal layout group. Why, when I set autosize for the text, the autosize will change the rect width. The autosize should increase the size of text until the text will reach the borders of the rect, nothing else So I don’t understand this buggy behavior when the text is changing rect width when rect width is forced to be driven only by horizontal layout group

As a result of this bug, the Preferred Width of UI Text objects when using Best Fit with the point size set at the min of the best fit range will always be incorrect and much smaller than it should be. As such since the preferred width of the text ends up being smaller than the width calculated by the Horizontal group with Child Force Expand factoring in the number of child objects and space to fill, the size of the Rect doesn’t change.

Again, the width of the Rect of the text doesn’t change because the preferred width is incorrect and value too small. If you disable Best Fit and set the point size of the text to 300, you will see the affect of the correct preferred width on it all.

So since this bug results in a preferred width that is super small and incorrect in all cases, it is the same as overriding the preferred width of the TMP text object to use a value of zero will results in the same exact behavior as UI Text.

I understand, but using the best fit is an essential part of our multi-resolution UI pipeline. Is there a plan to fix the bug?

If you are using TextMesh Pro, you can use Auto Size but will have to override the preferred width to set it to zero which will make TMP work just like UI Text.

If you are asking about fixing the UI Text bug with Best Fit, there is no plan to fix it. Furthermore, fixing it would make UI Text when Best Fit is enabled behave just like TextMesh Pro.

Again, setting the Preferred Width to 0 on the text object’s Layout Element which still enables you to use Flexible Width, will work just like UI Text and the behavior you seek.