Skip to content

Instantly share code, notes, and snippets.

@hulefei
Created January 14, 2020 10:13
Show Gist options
  • Save hulefei/b39f1cb7f76886264964398d1edd8466 to your computer and use it in GitHub Desktop.
Save hulefei/b39f1cb7f76886264964398d1edd8466 to your computer and use it in GitHub Desktop.
计算string长度
public float TextStringWidth(Text mText)
{
var font = mText.font;
var fontsize = mText.fontSize;
var text = mText.text;
var fontStyle = mText.fontStyle;
font.RequestCharactersInTexture(text, fontsize, fontStyle);
var width = 0f;
for (var i = 0; i < text.Length; i++)
{
CharacterInfo characterInfo;
font.GetCharacterInfo(text[i], out characterInfo, fontsize);
width += characterInfo.advance;
}
return width;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment