unity 获取Text组件里text内容的长度

/// 
    /// 计算字符串在指定text控件中的长度
    /// 
    /// 
    /// 
    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;
    }

 

你可能感兴趣的:(unity)