TextMeshPro 填坑

最近在项目里碰到字体开启描边后缩放很小时,会有清晰的轮廓线,查了半天也没弄明白到底因为啥,最后看了 Unity 的论坛发现, Padding 最好设置为当前子大小的 10%,也就是下面的第二条

这是和我碰到同样问题的小哥贴的图

原文^1

requently Asked Questions (in the process of being revised / edited)

1. When I change the material properties of a text object, it affects the other text objects using that same font asset. How do I control what objects are affected by material property changes?

All Font Assets contain a Default Material which gets assigned to the text objects that are using this font asset. Since these text objects share the same material, changes to the material properties of any of them is reflected on all of them. In order to have different visual styles on these objects, you will need to use different Material Presets or instances of the current material assigned to those objects. See the following updated video about Creating & Working with Material Presets.

2. I see visual artifacts around the edges of characters. How do I get rid of them?

These artifacts can be caused by material properties like Dilation, Outline, Underlay, etc. As these property values get near their maximum range, parts of adjacent characters in the font atlas texture can bleed into each other.

Most material properties are normalized (0, 1) or (-1, 1). As such, the effective range of these material properties is determined by the Ratio of Sampling Point Size to Padding. The larger the ratio the more effective range these properties will have. Ie. the thicker the Outline or Underlay Offset range you will have.

These visual artifacts are the result of the Sampling Point Size to Padding ratio being too small and usually combined with material property values near their maximum range. To resolve this issue simply regenerate the font asset to increase this ratio.

A good ratio to use by default is 10%. See the following updated video about Font Asset Creation which provides information about padding and how it affects material properties and their range.

3. Characters seems to be cut off near their edges, especially when zooming out. How do I fix this?

By default, character sprites are fit tightly around their visible shape. This minimizes overdraw. However, this can cause the described effect. To solve this, enable extra padding in the font settings panel of your text object.

4. Why is the data contained in the TextMeshPro.textInfo class invalid after I just set the text via script?

For optimization purposes, changes to any of the properties of the text object are processed as a group once per frame which happens just before the frame is rendered. Once the text object is re-generated the text object and content of the TextMeshPro.textInfo will be updated. Most of the time, this is fine however for those times where you need a text object to be updated right away, you can use the ForceMeshUpdate() function.

5. My materials appear to be broken. How do I fix them?

Have a look at this video.

6. How can I use UTF-16 and UTF-32 characters?

You have to use escape codes for such characters. For UTF-16, use codes like \uF199. For UTF-32, use codes like \U00FF00FF. You can add them to the font atlas explicitly in the same way, or use unicode ranges.

7. I have many dynamic text objects with auto-size and the performance is bad. How do I improve this?

Auto-size is expensive, because text layout needs to be performed multiple times to find the best fit point size. If the text is static or remains about the same length, it would be more efficient to only use Auto-size when the text object is first instantiated to find the best fit point size. Thereafter simply disable the Auto-size and manually set the point size via script.

8. Why can't I get the text component via GetComponent()?

You can, but there are actually two different types of TextMeshPro components.

The normal TMP component is of type which can be access using GetComponent(). The UI TMP component is of type which can be accessed via GetComponent().

Both TextMesh Pro components inherit from a base class which is TMP_Text where you can also get a reference to either types of TextMesh Pro components by using GetComponent<TMP_Text>().

To access these components in your scripts you will also need to include the "using TMPro;" namespace.

9. How can I assign a different material preset to my text object?

You can apply a different material preset by dragging it from the project view onto the material inspector of the text object. To learn more about creating and using Material Presets, see the following video.

10. How can I increase the range of text effects like dilation, outline, and underlay?

The effective range of these material properties is determined by the Sampling Point Size to Padding ratio when creating the font asset. The larger the ratio, the more effective range you will have. Of course a larger ratio means a larger padding value which means less room for character data. So there is a trade-off between character sampling quality and visual effect range. Use the smallest padding that works for you. Note that the padding is specified in pixels, so if you double the texture size you also have to double the padding to keep the same range. See here and here for more details. See the following updated video about Font Asset Creationwhich provides information about padding and how it affects material properties and their range.

11. Can I use drop caps?
While there is no special support for drop caps, you can create them with some creative use of rich tags. See this topic for details.

12. Can I put text on a curve?

Yes. See the following thread.

13. Why doesn't the font tag load my custom font?

Your font asset has to be placed in a Resources folder and more specifically at the location specified in the TMP Settings. The default location for font assets specified in the TMP Settings is "Resources/Fonts & Materials".

14. Why is a tag rendered as text, instead of being used for rich text?

When a tag doesn't work, it is shown as plain text. This is done so you can see that there is something wrong and can identify the problem. Failure can be caused by invalid tags, missing or invalid tag arguments, or because sprite or font resources could not be located.

15. Text displays fine in the Editor but on some platforms, the text from some objects is not visible. What could be causing this?

The most common source of this issue is using a mixture of and objects that happen to share the same font asset or material preset. This behavior is due to the Mesh Renderer and Canvas Renderer fighting over the ZTest of the shared material where on some platforms some of the text objects become invisible.

For example, when the Canvas render mode is set to Overlay, the ZTest on the material would be set to Always whereas for an object in World Space, it would be set to LEqual. Since a material can't be both values at once, the renderer who set the value last typically wins. There is more to this but that is the essence of it.

A simple way to address this issue is to use a separate set of Material Presets for the text objects using the Canvas system and another for the text objects using the Mesh Renderer.

参考