[UGUI] How to configure and use Chinese fonts in TextMeshPro

        In Unity, TextMeshPro provides more advanced text rendering functions than conventional Text components. TextMesh Pro can handle various languages, including Chinese. We can easily use Chinese in Unity projects without worrying about font and layout issues.

Table of contents

1. Steps to configure and use Chinese fonts

2. Warning and solution

3. Introduction to Font Asset Creator properties


1. Steps to configure and use Chinese fonts

        The first step is to obtain Chinese font files from a font library or other sources, usually in .ttf, .otf or .ttc format. The easiest way is to drag a .ttc file from the Font folder of Windows system files (C drive > Windows folder > Fonts folder). Here is a Chinese font download resource: Font/font at master kaienfr/Font GitHub

        The second step is to create a new text file, name it "My Font", open the .txt file, and edit the text. The blogger will use "Hello, world!" as an example. In the actual application of the project, it is best for the editors to join All commonly used Chinese characters can be directly used in Baidu Commonly Used Chinese Character Library. Here is the download link: Font/Common Chinese Character Library 3500.txt at master kaienfr/Font GitHub

        The third step is to copy the font files and font library files to the Assets folder of the Unity project or its subfolders.

        The fourth step, in Unity, click Window, select TextMeshPro > Font Asset Creator to create a new font resource (all properties of Font Asset Creator are explained at the end of the article), drag the font file into Source Font File, and set Atlas Resolution to Maximum, set the Character Set property to Characters from File, then drag the font file into Character File, and set the rest of the properties according to your needs, click Generate Font Atlas to start generating the font atlas texture, wait for a while, click Save, and save it to Assets folder or its subfolders.

        Step 5: Right-click on the hierarchy panel to create TextMeshPro (GameObject > UI > Text - TextMeshPro), drag the saved file into the inspection panel, edit the text "Hello, world!", and then a series of warnings will appear for these Warning, the blogger provides a solution in the second part.

        The effect is as follows:

         In the sixth step, friends and bloggers may say "Hello, world!" Each character has a layer of white translucent shading, this is because TextMeshPro uses a directed distance field for rendering, solution: Text ( TMP) material's Shader property is set to Bitmap.

         The effect is as follows:

2. Warning and solution

        警告1:Unable to add the requested character to font asset [msyhbd SDF]'s atlas texture. Please make the texture [msyhbd SDF Atlas] readable.

        Solution 1: Select the atlas texture contained in the font in the project panel, then change the inspector to Debug mode and check Is Readable.

        Solution 2: Open the .asset file with Notepad in the font file directory, find m_Isreadable:0, change it to 1 and save it, the principle is the same as solution 1, refer to: Unity TMPro Unable to add the requested character to font asset - 哔哩哔哩

        警告2:The character used for Underline is not available in font asset [msyhbd SDF].

        Solution 1: Find the TextMeshPro file in the project panel, select Resources > TMP Settings, and check Disable warnings in the inspection panel, that is, turn off the warning. Solution 2 is given below to fundamentally fix this warning.

        Solution 2: The warning translates to: "The character used for underline is not available in the font asset [msyhbd SDF]", that is, the underline style cannot be used. For this warning, just add the underline character to the character set and repeat the above steps. Can. Reference: Unity notes warn The character used for Underline is not available in f - 哔哩哔哩

        警告3:“The character with Unicode value \u3002 was not found in the [msyhbd SDF] font asset or any potential fallbacks. It was replaced by Unicode character \u25A1 in text object [Text (TMP)].”

        Solution: The warning translates to: "The character with Unicode value \u3002 was not found in the [msyhbd SDF] font resource or any potential fallback. It was replaced by the Unicode character \u25A1 in the text object [Text (TMP)] "That is, we have entered characters that are not in the font text, the solution can be the same as warning 2 solution 1, turn off the warning, so that even if we enter characters that are not in the font text, only the Unicode character \u25A1 will appear on the Scene interface, There will be no warnings, which is convenient for project debugging.

3. Introduction to Font Asset Creator properties

        Unity has officially explained all properties ( Font Asset Creator | TextMeshPro | 4.0.0-pre.2 ), for the convenience of your friends, the translation is explained as follows:

        Source Font File: Select the font used to generate Text Mesh Pro font resources.

        Sampling Point Size: Sets the font size (in points) used to generate the font texture.

                Auto Sizing (default): Use the largest point size possible while still fitting all characters on the texture.        

                Custom Size: Use a custom point size, enter the desired size in the text box, use this setting for pixel-precise control over bitmap-only fonts.

        Padding: Specifies the spacing in pixels between characters in the font texture. The larger the fill, the smoother the transition, allowing higher quality rendering and larger effects such as thick outlines.

        Packing Method: Specifies how to fit characters to the font texture.

                Optimum: Finds the largest possible auto font size that still fits all characters in the texture.

                Fast (default): Compute character packs faster, but may use smaller font sizes than optimized mode.

        Atlas Resolution: Sets the size width and height of the font texture in pixels. When using SDF fonts, higher resolutions produce finer gradients, resulting in higher quality text.

        Character Set: Select a predefined character set.

                ASCII (default): Includes visible characters in the ASCII character set.

                Extended ASCII: Includes visible characters in the extended ASCII character set.

                ASCII Lowercase: Contains only lowercase characters that are visible in the ASCII character set.

                ASCII Uppercase: Contains only uppercase characters visible in the ASCII character set

                Numbers+Sysbols: Includes only visible numbers and symbols from the ASCII character set.

                Custom Range: Enter a series of decimal values ​​or a range of values ​​to specify the characters to include.

                Unicode Range(Hex): Enter a range of unicode hexadecimal values ​​or a range of values ​​to specify the characters to include.

                Custom Characters: Enter a character sequence to specify the characters to include.

                Characters from File: Include all the characters in the text resource specified by the friends.

        Render Mode: Specifies the rendering mode to use when outputting the font atlas.

                SMOOTH: Renders the atlas as an antialiased bitmap.

                RASTER: Renders the atlas as a non-antialiased bitmap.

                SMOOTH_HINTED: Renders the atlas as an antialiased bitmap and aligns character pixels to texels for sharper results.

                RASTER_HINTED: Renders the atlas as a non-antialiased bitmap and aligns character pixels to texels for sharper results.

                SDF: Atlases are rendered using the slower but more accurate SDF generation mode, and without oversampling.

                SDFAA: Renders atlases using a faster but less accurate SDF generation mode, which produces font atlases sufficient for most situations.

                SDFAA_HINTED: Renders atlases using a faster but less accurate SDF generation mode, and aligns character pixels to texels for sharper results. It generates font atlases that are sufficient for most cases.

                SDF8: Renders the atlas with a slower but more accurate SDF generation mode and 8x oversampling.

                SDF16: Renders the atlas with a slower but more accurate SDF generation mode and 16x oversampling.

                SDF32: Renders the atlas with a slower but more accurate SDF generation mode and 32x oversampling. Use this setting for fonts with complex or small characters.

        Get Kerning Pairs: Enable this option to copy kerning data from the font.

        Generate Font Atlas: Generate font atlas texture.

Guess you like

Origin blog.csdn.net/m0_51942776/article/details/130411475