ContentFitText.cs 3.4 KB

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