Unity custom fonts include Chinese

A lot of artistic characters are needed in the project. I checked it on the Internet and can be made with BmFont. However, to cut out the pictures individually and import them one by one to set the id size and so on. It is really cumbersome to fry chicken. Later others shared another plug-in. It’s made by me, it’s awesome and easy to use,  but the link still can’t escape. It’s very troublesome to type one by one. So I made a tool

Effect picture

    

1. Principle

    Unity has always had a custom font function, and many blogs can also be found online.

Then we just need to set the relevant information

Set the corresponding size as many texts as there are

Index: is the character decimal index

UV: UV information of the text in the picture

Vert: The vertical size depends on the pixel size of the characters. For example, if your characters are 128x128, input 128 and -128 in Vert Width and Height respectively to get the proper proportion of letters. Vertical Y must be negative.

Advance : The required horizontal distance from the origin of this character to the origin of the next character, which is almost wide

Mainly the Index of each word

For details, you can look at Unity's official document and another article

https://docs.unity3d.com/Manual/class-Font.html

http://www.manew.com/thread-110484-1-1.html

For example, when we make a number (0-9) font, we need a digital picture, and then set it Character Rects. The document says clearly, Indexit refers to the index (decimal) of the ASCII code of the current word, such as 0=48 1= 49...

If we want to set more text, it is also possible, because Unity supports Unicode, but we need some steps,将文字->16进制->十进制

Here is an online conversion tool: http://www.ab126.com/goju/1711.html

For coding, you can check this article: http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html

Code

string content = "你好";
    for (int i = 0; i < content.Length; i++)
    {
        var bytes = Encoding.Unicode.GetBytes(content[i].ToString());
        var stringBuilder = new StringBuilder();
        for (var j = 0; j < bytes.Length; j += 2)
        {
            //x2是十六进制 两位如果没有用 0补充
            stringBuilder.AppendFormat("{0:x2}{1:x2}", bytes[j + 1], bytes[j]);
        }
        Debug.Log(stringBuilder.ToString());
        //你->4f60
        //好->597d
        int index = Convert.ToInt32(stringBuilder.ToString(), 16);
        Debug.Log(index);
        //你->20320
        //好->22909
    }

or

 string str = "你好";
        for (int i = 0; i < str.Length; i++)
        {
            Debug.Log(System.Convert.ToInt32(str[i]));
            //log: 20320
            //log: 22909
        }

 

Then we set the "you" Index of the font to 20320.

2. What needs to be prepared

  1. A text text must be in utf-8 format, and the text content inside
  2. You want a word art picture, the picture size is defined arbitrarily, but you want a regular picture and you need to know the length and width of each picture word
  3. Try to keep the picture as neat as possible

3. How to use

  1.  

    

    Finally, click Generate. I made two examples in the package (the pictures are not good-looking and light spray)

  4. Attention

  1. The word order of text and picture should be one-to-one correspondence
  2. There is a very fatal problem. I used 5.6.2 to develop at the time. There is no problem, but if it is lower than 5.6.2, the font generated by the font will not be displayed. At this time, just copy (Ctrl+D) a copy of the generated font. Okay, this is estimated to be an official bug

 

Any other questions directly ask on github

Attach address: https://github.com/ZeroUltra/Custom-Font

    

Guess you like

Origin blog.csdn.net/K20132014/article/details/80462509