An overview of Unity's TextMeshPro usage

1. Generate dynamic fonts

1. Font file: such as in the fonts folder of the windows system

2.导入unity,create——Text Mesh Pro——Font Asset

3. Font supplement: create a txt file, include commonly used Chinese characters (Chinese punctuation marks should also be handled by yourself), import into unity, then select the generated SDF font, and update the font atlas

4. Select the appropriate resolution, too small will cause the font to be blurred when enlarged, choose to import characters from the file, Generate, and then Save

5. Test the generated font, hey!

2. Static expressions

1. TMP supports graphic and text mixed layout, use sprite tag (such as <sprite=1>) default emoticon set in

2. Make font expression atlas

a. Resource atlas, segmentation

b. Generate TMP Sprite Asset

c. Modify location information

d. Modify the default emoticon set of TMP setting

You can also specify the atlas name in the sprite tag:

The <sprite> tag inserts the picture in the Sprite Asset into the text, and the Sprite Asset must be located in the specified folder TextMeshPro -> Resources -> Sprite Assets in TMP .
<sprite=9>//default atlas + index

<sprite name="happy"> //Default atlas + image name

<sprite="My Sprites" index=9> //Specify the atlas + index

<sprite="My Sprites" name="happy">//Specify the image gallery + image name

3. Dynamic expressions

TMP can specify multiple attributes in the sprite, and anim can specify the start index, end index, and playback rate of the playback atlas, such as <sprite=1 anim=1,5,10>

5. Test the generated font, hey!

<sprite=0><sprite=1><sprite=2><sprite=3>

<sprite="emoji" index=0><sprite="emoji" index=1>

<sprite="emoji" anim=0,3,10>

<sprite index=1 anim=1,5,10>

<sprite="EmojiOne" index=1 anim=1,5,5>

4. Hyperlinks

<link="ID">Hyperlink</link>, you need to listen to click events and do corresponding processing:

public void OnPointerClick(PointerEventData eventData)
{
    TMP_Text tmp = GetComponent<TMP_Text>();
    int linkIndex =TMP_TextUtilities.FindIntersectingLink(tmp,eventData.position,null);
    if (linkIndex != -1)
    {
        TMP_LinkInfo linkInfo = tmp.textInfo.linkInfo[linkIndex];
        Debug.Log(linkInfo.GetLinkID());
        Application.OpenURL(linkInfo.GetLinkID());
    }
}

5. 3D text

6. Performance optimization

1. Since the format of the font atlas exported by TMP is uncompressed, it takes up 16MB of memory for the pure Alpha channel of 4096 * 4096, so consider compressing the atlas

2. Using graphics and text to mix pictures and text will take up a dc respectively (because the material information used by the two is different). Therefore, the mixed picture in TMP should also be careful not to overlap with the font, otherwise it will interrupt the merging process of the text itself and increase the dc. If there is overlap, put the picture on the upper layer (it seems that the level of the picture in the same component will be higher than the text by default), so that the dc between the text and the text is still merged

3. The outer glow, lighting, and shadow features supported by the TMP component are all implemented in the shader. If they are fully enabled, it will inevitably increase the rendering overhead. It is recommended to choose a different shader to generate fonts according to the project situation, such as the shader in the mobile provided by the plug-in (the shader in the mobile not only deletes some functions such as light and light, but also simplifies the algorithm of the outer contour and shadow)

Guess you like

Origin blog.csdn.net/Yongjun_Ren/article/details/126623173