How does unity get the length of text content in the text component


I won’t talk more nonsense. Let’s look at the code directly~

/// <summary>
    ///Calculates the length of the string in the specified text control
    /// </summary>
    /// <param name="message"></param>
    /// <returns></returns>
    int CalculateLengthOfText(string message,Text tex)
        int totalLength = 0;
        Font myFont = tex.font;  //chatText is my Text component
        myFont.RequestCharactersInTexture(message, tex.fontSize, tex.fontStyle);
        CharacterInfo characterInfo = new CharacterInfo(); 
        char[] arr = message.ToCharArray(); 
        foreach (char c in arr)
            myFont.GetCharacterInfo(c, out characterInfo, tex.fontSize); 
            totalLength += characterInfo.advance;
        return totalLength;

Supplement: use textasset of unity to read the content of TXT document and store the item information in the dictionary

Text asset text asset

Text Assets are a format for imported text files. When you drop a text file into your Project Folder, it will be converted to a Text Asset. The supported text formats are:

Text resource is a format of imported text file. When you drag a text file into your project, it will be converted into a text resource. Supported text formats are:

.txt .html .htm .xml .bytes

Properties property


The full text of the asset as a single string.

Item attribute analysis

After that, you need to create a new text txt to store the attributes of the item. Here, take medicine as an example

Txt document content

Each line of text corresponds to an item, separated by commas, and each value corresponds to each column of attributes in the attribute table.

Then create a new c# script to parse the text content

private TextAsset objectsInfoListText; //  New textasset variable
private Dictionary<int, ObjectInfo> objectInfoDict = new Dictionary<int,ObjectInfo>();
void Awake()
    objectsInfoListText = Resources. Load("ObjectsInfoList") as TextAsset; //  Read the file from the path of resources
void ReadInfo()
     string text = objectsInfoListText. text;  //  Read the text content as a string
     string[] objInfoArray = text. Split('\n'); //  Splits text into an array with \ n as the delimiter
     Foreach (string STR in objinfoarray) // traverse each line and put the value of each drug into the class object and store it in the dictionary
         ObjectInfo info = new ObjectInfo();
         string[] propertyArray = str.Split(',');
         int id = int.Parse(propertyArray[0]);
         string name = propertyArray[1];
         string icon_name = propertyArray[2];
         string str_type = propertyArray[3];
         ObjectType type = ObjectType.Drug;
         switch (str_type)
             case "Equip":
                 type = ObjectType.Equip;
             case "Mat":
                 type = ObjectType.Mat;
             case "Drug":
                 type = ObjectType.Drug;
         } = id; = name; 
         info.icon_name = icon_name; info.type = type;
         if (type == ObjectType.Drug)
             int hp = int.Parse(propertyArray[4]);
             int mp = int.Parse(propertyArray[5]);
             int price_sell = int.Parse(propertyArray[6]);
             int price_buy = int.Parse(propertyArray[7]);
             info.hp = hp; = mp; info.price_sell = price_sell; info.price_buy = price_buy;
         //Store item information in a dictionary
         objectInfoDict.Add(id, info);
public enum ObjectType
public class ObjectInfo 
    public int id;
    public string name;
    public string icon_name;
    public ObjectType type;
    public int hp;
    public int mp;
    public int price_sell;
    public int price_buy;

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer. If there are mistakes or not fully considered, please don’t hesitate to comment.