源战役客户端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

150 rindas
5.9 KiB

pirms 1 mēnesi
  1. /*
  2. author:Saber
  3. date:2020/4/6
  4. desc:
  5. */
  6. using System;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. namespace LuaFramework{
  11. [AddComponentMenu("UI/AdaptiveText", 16)]
  12. [ExecuteInEditMode]
  13. public class AdaptiveText : Text
  14. {
  15. protected AdaptiveText()
  16. {
  17. useLegacyMeshGeneration = false;
  18. }
  19. public bool need_adaptive = false;
  20. [Range(1, 10)]
  21. public int min_size = 10;
  22. [Range(11, 70)]
  23. public int max_size = 40;
  24. // 文本赋值修改
  25. public override string text{
  26. get
  27. {
  28. return base.m_Text;
  29. }
  30. set
  31. {
  32. if (String.IsNullOrEmpty(value))
  33. {
  34. if (String.IsNullOrEmpty(base.m_Text))
  35. return;
  36. base.m_Text = "";
  37. SetVerticesDirty();
  38. }
  39. else if (base.m_Text != value)
  40. {
  41. base.m_Text = value;
  42. SetVerticesDirty();
  43. SetLayoutDirty();
  44. // 重新设置文本大小
  45. FitFontSize();
  46. }
  47. }
  48. }
  49. // 字体大小设定,在自适应大小文本的情况下拒绝外部修改
  50. new public int fontSize
  51. {
  52. get
  53. {
  54. return base.fontSize;
  55. }
  56. set
  57. {
  58. if (need_adaptive)
  59. return;
  60. base.fontSize = value;
  61. }
  62. }
  63. protected override void OnEnable()
  64. {
  65. base.OnEnable();
  66. FontUpdateTracker.TrackText(this);
  67. FitFontSize();
  68. }
  69. protected override void OnDisable()
  70. {
  71. base.OnDisable();
  72. FontUpdateTracker.UntrackText(this);
  73. }
  74. // 文本内容变动回调
  75. private void OnTextureRebuilt(Font font){
  76. if(font == null) return;
  77. FitFontSize();
  78. }
  79. private void FitFontSize(){
  80. if(!need_adaptive) return;
  81. base.fontSize = base.fontSize < min_size ? min_size : base.fontSize;
  82. base.fontSize = base.fontSize > max_size ? max_size : base.fontSize;
  83. bool pass = CheckTextSizeFitOrNot();
  84. // 先检测缩小,再检测放大
  85. int last_fontSize = base.fontSize;
  86. while(!pass && base.fontSize > min_size){
  87. base.fontSize --;
  88. pass = CheckTextSizeFitOrNot();
  89. }
  90. // 字体没变化说明已经够小,要处理放大到合适大小
  91. // 这么写的原因是要获取到对应字体的真实preferredHeight,这个接口是私有的外面调不到,而且还得构造数据
  92. if (last_fontSize == base.fontSize && base.fontSize < max_size){
  93. base.fontSize ++;
  94. pass = CheckTextSizeFitOrNot();
  95. while(pass && base.fontSize <= max_size){
  96. base.fontSize++;
  97. pass = CheckTextSizeFitOrNot();
  98. }
  99. // 因为字体大小不满足被跳出,则需要减一号字号
  100. base.fontSize--;
  101. }
  102. }
  103. // private Font m_font;
  104. // private CharacterInfo characterInfo;
  105. // 原先的写法是针对字体的字符类型为dynamic动态型的计算方式,但是如果把字体类型改为Unicode的话,字符的advance会固定为某个数值,这个情况下preferredWidth反而是正确的结果
  106. // 目前项目中主要的字体都不是dynamic字符类型了,但也要保留这部分的代码,后续如果有不同字符类型的字体出现的话就可以使用判断区分计算方式
  107. // 相关的字段名为TrueTypeFontImporter类中的m_ForceTextureCase字段,枚举FontTextureCase
  108. // private bool CheckTextSizeFitOrNot(int fontSize){
  109. private bool CheckTextSizeFitOrNot(){
  110. // bool pass = false;
  111. // m_font = base.font;
  112. // float m_rect_width = this.gameObject.GetComponent<RectTransform>().rect.width;
  113. // float m_rect_height = this.gameObject.GetComponent<RectTransform>().rect.height;
  114. // float text_width = 0f;
  115. // if(font != null){
  116. // font.RequestCharactersInTexture(base.text, fontSize, base.fontStyle);
  117. // for(int i = 0; i <base.text.Length; i++)
  118. // {
  119. // font.GetCharacterInfo(base.text[i], out characterInfo, fontSize);
  120. // text_width += characterInfo.advance;
  121. // }
  122. // Debug.Log("base.preferredWidth:" + base.preferredWidth);
  123. // Debug.Log("base.preferredHeight:" + base.preferredHeight);
  124. // 必须文本的宽度和高度在限制内才可以通过测试
  125. // pass = text_width <= m_rect_width && base.preferredHeight <= m_rect_height;
  126. // }
  127. // return pass;
  128. float m_rect_width = this.gameObject.GetComponent<RectTransform>().rect.width;
  129. float m_rect_height = this.gameObject.GetComponent<RectTransform>().rect.height;
  130. return base.preferredWidth <= m_rect_width && base.preferredHeight <= m_rect_height;
  131. }
  132. // 手动自适应排版
  133. public void FitFontSizeManul(){
  134. FitFontSize();
  135. }
  136. // 获取文本的实际宽度
  137. public float GetContentWidth()
  138. {
  139. float m_rect_width = this.gameObject.GetComponent<RectTransform>().rect.width;
  140. // 如果文本宽度超过UI大小,则返回UI本身的宽度,否则返回文本宽度
  141. return base.preferredWidth <= m_rect_width ? base.preferredWidth : m_rect_width;
  142. }
  143. }
  144. }