KeyboardUtils.cs 700 B

1234567891011121314151617181920212223242526
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class KeyboardUtils
  6. {
  7. public static int CaculateTextLength(string message, Text tex)
  8. {
  9. int totalLength = 0;
  10. Font myFont = tex.font; //chatText is my Text component
  11. myFont.RequestCharactersInTexture(message, tex.fontSize, tex.fontStyle);
  12. CharacterInfo characterInfo = new CharacterInfo();
  13. char[] arr = message.ToCharArray();
  14. foreach (char c in arr)
  15. {
  16. myFont.GetCharacterInfo(c, out characterInfo, tex.fontSize);
  17. totalLength += characterInfo.advance;
  18. }
  19. return totalLength;
  20. }
  21. }