Created
January 14, 2020 10:13
-
-
Save hulefei/b39f1cb7f76886264964398d1edd8466 to your computer and use it in GitHub Desktop.
计算string长度
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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