ContentFitText.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. namespace SRF.UI
  6. {
  7. [ExecuteInEditMode]
  8. [RequireComponent(typeof(RectTransform))]
  9. [AddComponentMenu("SRF/UI/Content Fit Text")]
  10. public class ContentFitText : UIBehaviour, ILayoutElement
  11. {
  12. public float minWidth
  13. {
  14. get
  15. {
  16. if (this.CopySource == null)
  17. {
  18. return -1f;
  19. }
  20. return LayoutUtility.GetMinWidth(this.CopySource.rectTransform) + this.Padding.x;
  21. }
  22. }
  23. public float preferredWidth
  24. {
  25. get
  26. {
  27. if (this.CopySource == null)
  28. {
  29. return -1f;
  30. }
  31. return LayoutUtility.GetPreferredWidth(this.CopySource.rectTransform) + this.Padding.x;
  32. }
  33. }
  34. public float flexibleWidth
  35. {
  36. get
  37. {
  38. if (this.CopySource == null)
  39. {
  40. return -1f;
  41. }
  42. return LayoutUtility.GetFlexibleWidth(this.CopySource.rectTransform);
  43. }
  44. }
  45. public float minHeight
  46. {
  47. get
  48. {
  49. if (this.CopySource == null)
  50. {
  51. return -1f;
  52. }
  53. return LayoutUtility.GetFlexibleHeight(this.CopySource.rectTransform) + this.Padding.y;
  54. }
  55. }
  56. public float preferredHeight
  57. {
  58. get
  59. {
  60. if (this.CopySource == null)
  61. {
  62. return -1f;
  63. }
  64. return LayoutUtility.GetPreferredHeight(this.CopySource.rectTransform) + this.Padding.y;
  65. }
  66. }
  67. public float flexibleHeight
  68. {
  69. get
  70. {
  71. if (this.CopySource == null)
  72. {
  73. return -1f;
  74. }
  75. return LayoutUtility.GetFlexibleHeight(this.CopySource.rectTransform);
  76. }
  77. }
  78. public int layoutPriority
  79. {
  80. get
  81. {
  82. return 0;
  83. }
  84. }
  85. public void CalculateLayoutInputHorizontal()
  86. {
  87. this.CopySource.CalculateLayoutInputHorizontal();
  88. }
  89. public void CalculateLayoutInputVertical()
  90. {
  91. this.CopySource.CalculateLayoutInputVertical();
  92. }
  93. protected override void OnEnable()
  94. {
  95. this.SetDirty();
  96. this.CopySource.LayoutDirty += this.CopySourceOnLayoutDirty;
  97. }
  98. private void CopySourceOnLayoutDirty(SRText srText)
  99. {
  100. this.SetDirty();
  101. }
  102. protected override void OnTransformParentChanged()
  103. {
  104. this.SetDirty();
  105. }
  106. protected override void OnDisable()
  107. {
  108. this.CopySource.LayoutDirty -= this.CopySourceOnLayoutDirty;
  109. this.SetDirty();
  110. }
  111. protected override void OnDidApplyAnimationProperties()
  112. {
  113. this.SetDirty();
  114. }
  115. protected override void OnBeforeTransformParentChanged()
  116. {
  117. this.SetDirty();
  118. }
  119. protected void SetDirty()
  120. {
  121. if (!this.IsActive())
  122. {
  123. return;
  124. }
  125. LayoutRebuilder.MarkLayoutForRebuild(base.transform as RectTransform);
  126. }
  127. public SRText CopySource;
  128. public Vector2 Padding;
  129. }
  130. }