AppUILocalizationTextBreak.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System.Text;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace AppUI.Localization
  6. {
  7. /// <summary>
  8. /// 按 UI 可用宽度在文案中插入零宽空格 <c>\u200B</c>,配合 TMP / Legacy Text 自动换行。
  9. /// </summary>
  10. static class AppUILocalizationTextBreak
  11. {
  12. const char ZeroWidthSpace = '\u200B';
  13. public static float ResolveMaxWidth(RectTransform rect, float maxWidth)
  14. {
  15. if (maxWidth > 0f)
  16. return maxWidth;
  17. if (rect == null)
  18. return 0f;
  19. float width = rect.rect.width;
  20. if (width > 0f)
  21. return width;
  22. return LayoutUtility.GetPreferredWidth(rect);
  23. }
  24. public static float ResolveMaxHeight(RectTransform rect, float maxHeight)
  25. {
  26. if (maxHeight > 0f)
  27. return maxHeight;
  28. if (rect == null)
  29. return 0f;
  30. float height = rect.rect.height;
  31. if (height > 0f)
  32. return height;
  33. return LayoutUtility.GetPreferredHeight(rect);
  34. }
  35. public static string InsertWidthBreaks(string text, TMP_Text tmpText, float maxWidth, float maxHeight = 0f)
  36. {
  37. if (string.IsNullOrEmpty(text) || tmpText == null)
  38. return text;
  39. maxWidth = ResolveMaxWidth(tmpText.rectTransform, maxWidth);
  40. if (maxWidth <= 0f)
  41. return text;
  42. if (FitsSingleLine(text, tmpText, maxWidth))
  43. return text;
  44. var output = new StringBuilder(text.Length + 16);
  45. InsertWidthBreaksInto(text, output, maxWidth, line => tmpText.GetPreferredValues(line, 0, 0).x);
  46. string result = output.ToString();
  47. maxHeight = ResolveMaxHeight(tmpText.rectTransform, maxHeight);
  48. if (maxHeight > 0f && tmpText.GetPreferredValues(result, maxWidth, 0).y > maxHeight)
  49. return result;
  50. return result;
  51. }
  52. public static string InsertWidthBreaks(string text, Text legacyText, float maxWidth, float maxHeight = 0f)
  53. {
  54. if (string.IsNullOrEmpty(text) || legacyText == null)
  55. return text;
  56. maxWidth = ResolveMaxWidth(legacyText.rectTransform, maxWidth);
  57. if (maxWidth <= 0f)
  58. return text;
  59. float Measure(string line)
  60. {
  61. var settings = legacyText.GetGenerationSettings(new Vector2(maxWidth, 0f));
  62. settings.generateOutOfBounds = true;
  63. return new TextGenerator().GetPreferredWidth(line, settings) / legacyText.pixelsPerUnit;
  64. }
  65. if (Measure(text) <= maxWidth)
  66. return text;
  67. var output = new StringBuilder(text.Length + 16);
  68. InsertWidthBreaksInto(text, output, maxWidth, Measure);
  69. string result = output.ToString();
  70. maxHeight = ResolveMaxHeight(legacyText.rectTransform, maxHeight);
  71. if (maxHeight > 0f)
  72. {
  73. var settings = legacyText.GetGenerationSettings(new Vector2(maxWidth, maxHeight));
  74. settings.generateOutOfBounds = true;
  75. float height = new TextGenerator().GetPreferredHeight(result, settings) / legacyText.pixelsPerUnit;
  76. if (height > maxHeight)
  77. return result;
  78. }
  79. return result;
  80. }
  81. static bool FitsSingleLine(string text, TMP_Text tmpText, float maxWidth)
  82. {
  83. if (text.IndexOf('\n') >= 0)
  84. return false;
  85. Vector2 preferred = tmpText.GetPreferredValues(text, maxWidth, 0);
  86. if (preferred.x <= maxWidth)
  87. return true;
  88. return tmpText.GetPreferredValues(text, 0, 0).x <= maxWidth;
  89. }
  90. static void InsertWidthBreaksInto(string source, StringBuilder output, float maxWidth, System.Func<string, float> measureWidth)
  91. {
  92. for (int i = 0; i < source.Length; i++)
  93. {
  94. char c = source[i];
  95. if (c == '<')
  96. {
  97. int end = source.IndexOf('>', i);
  98. if (end > i)
  99. {
  100. output.Append(source, i, end - i + 1);
  101. i = end;
  102. continue;
  103. }
  104. }
  105. if (c == '\n')
  106. {
  107. output.Append(c);
  108. continue;
  109. }
  110. output.Append(c);
  111. while (GetVisibleLineLength(output) > 1 && measureWidth(GetCurrentLine(output)) > maxWidth)
  112. output.Insert(FindLastVisibleIndex(output), ZeroWidthSpace);
  113. }
  114. }
  115. static int FindLineStart(StringBuilder sb)
  116. {
  117. for (int i = sb.Length - 1; i >= 0; i--)
  118. {
  119. if (sb[i] == ZeroWidthSpace || sb[i] == '\n')
  120. return i + 1;
  121. }
  122. return 0;
  123. }
  124. static string GetCurrentLine(StringBuilder sb)
  125. {
  126. int start = FindLineStart(sb);
  127. return sb.ToString(start, sb.Length - start);
  128. }
  129. static int GetVisibleLineLength(StringBuilder sb)
  130. {
  131. string line = GetCurrentLine(sb);
  132. int count = 0;
  133. for (int i = 0; i < line.Length; i++)
  134. {
  135. if (line[i] == '<')
  136. {
  137. int end = line.IndexOf('>', i);
  138. if (end > i)
  139. {
  140. i = end;
  141. continue;
  142. }
  143. }
  144. count++;
  145. }
  146. return count;
  147. }
  148. static int FindLastVisibleIndex(StringBuilder sb)
  149. {
  150. int lineStart = FindLineStart(sb);
  151. for (int i = sb.Length - 1; i >= lineStart; i--)
  152. {
  153. if (sb[i] == ZeroWidthSpace || sb[i] == '\n')
  154. continue;
  155. if (sb[i] == '>')
  156. {
  157. int open = i;
  158. while (open >= lineStart && sb[open] != '<')
  159. open--;
  160. if (open >= lineStart)
  161. {
  162. i = open;
  163. continue;
  164. }
  165. }
  166. if (sb[i] == '<')
  167. continue;
  168. return i;
  169. }
  170. return sb.Length - 1;
  171. }
  172. }
  173. }