BetterText.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.Serialization;
  7. using UnityEngine.UI;
  8. namespace TheraBytes.BetterUi
  9. {
  10. #if UNITY_2018_3_OR_NEWER
  11. [ExecuteAlways]
  12. #else
  13. [ExecuteInEditMode]
  14. #endif
  15. [HelpURL("https://documentation.therabytes.de/better-ui/BetterText.html")]
  16. [AddComponentMenu("Better UI/Controls/Better Text", 30)]
  17. public class BetterText : Text, IResolutionDependency
  18. {
  19. public enum FittingMode
  20. {
  21. SizerOnly,
  22. StayInBounds,
  23. BestFit,
  24. }
  25. public FloatSizeModifier FontSizer { get { return customFontSizers.GetCurrentItem(fontSizerFallback); } }
  26. public FittingMode Fitting { get { return fitting; } set { fitting = value; CalculateSize(); } }
  27. public new float fontSize
  28. {
  29. get { return base.fontSize; }
  30. set { Config.Set(value, (o) => base.fontSize = Mathf.RoundToInt(o), (o) => FontSizer.SetSize(this, o)); }
  31. }
  32. [SerializeField]
  33. FittingMode fitting = FittingMode.StayInBounds;
  34. [FormerlySerializedAs("fontSizer")]
  35. [SerializeField]
  36. FloatSizeModifier fontSizerFallback = new FloatSizeModifier(40, 0, 500);
  37. [SerializeField]
  38. FloatSizeConfigCollection customFontSizers = new FloatSizeConfigCollection();
  39. bool isCalculatingSize;
  40. protected override void OnEnable()
  41. {
  42. base.OnEnable();
  43. CalculateSize();
  44. }
  45. public void OnResolutionChanged()
  46. {
  47. CalculateSize();
  48. }
  49. protected override void OnRectTransformDimensionsChange()
  50. {
  51. base.OnRectTransformDimensionsChange();
  52. CalculateSize();
  53. }
  54. public override void SetVerticesDirty()
  55. {
  56. base.SetVerticesDirty();
  57. CalculateSize();
  58. }
  59. void CalculateSize()
  60. {
  61. if (isCalculatingSize)
  62. return;
  63. isCalculatingSize = true;
  64. switch (fitting)
  65. {
  66. case FittingMode.SizerOnly:
  67. base.resizeTextForBestFit = false;
  68. base.fontSize = Mathf.RoundToInt(FontSizer.CalculateSize(this));
  69. break;
  70. case FittingMode.StayInBounds:
  71. base.resizeTextMinSize = Mathf.RoundToInt(FontSizer.MinSize);
  72. base.resizeTextMaxSize = Mathf.RoundToInt(FontSizer.MaxSize);
  73. base.resizeTextForBestFit = true;
  74. int size = Mathf.RoundToInt(FontSizer.CalculateSize(this));
  75. base.fontSize = size;
  76. base.Rebuild(CanvasUpdate.PreRender);
  77. int bestFit = base.cachedTextGenerator.fontSizeUsedForBestFit;
  78. base.resizeTextForBestFit = false;
  79. fontSize = (bestFit < size) ? bestFit : size;
  80. FontSizer.OverrideLastCalculatedSize(base.fontSize);
  81. break;
  82. case FittingMode.BestFit:
  83. base.resizeTextMinSize = Mathf.RoundToInt(FontSizer.MinSize);
  84. base.resizeTextMaxSize = Mathf.RoundToInt(FontSizer.MaxSize);
  85. base.resizeTextForBestFit = true;
  86. base.Rebuild(CanvasUpdate.PreRender);
  87. FontSizer.OverrideLastCalculatedSize(base.cachedTextGenerator.fontSizeUsedForBestFit);
  88. break;
  89. default:
  90. break;
  91. }
  92. isCalculatingSize = false;
  93. }
  94. #if UNITY_EDITOR
  95. protected override void OnValidate()
  96. {
  97. CalculateSize();
  98. base.OnValidate();
  99. }
  100. #endif
  101. }
  102. }