UIHelpItem.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using TMPro;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. [DisallowMultipleComponent]
  5. [RequireComponent(typeof(LayoutElement))]
  6. public class UIHelpItem : MonoBehaviour
  7. {
  8. [Header("Layout")]
  9. [SerializeField] float headerHeight = 60f;
  10. [SerializeField] float bodyTop = 68f;
  11. [SerializeField] float bottomPadding = 16f;
  12. RectTransform contentRoot;
  13. RectTransform bodyRect;
  14. RectTransform arrow;
  15. TMP_Text titleText;
  16. TMP_Text bodyText;
  17. Button headerButton;
  18. Image numberImage;
  19. GameObject divider;
  20. LayoutElement layoutElement;
  21. bool expandable;
  22. bool expanded;
  23. public Button HeaderButton => headerButton;
  24. public bool Expandable => expandable;
  25. public bool Expanded => expanded;
  26. void Awake()
  27. {
  28. layoutElement = GetComponent<LayoutElement>();
  29. if (layoutElement == null)
  30. layoutElement = gameObject.AddComponent<LayoutElement>();
  31. layoutElement.flexibleHeight = 0f;
  32. CacheReferences();
  33. }
  34. public void Configure(
  35. RectTransform content,
  36. bool canExpand,
  37. bool openByDefault,
  38. float header,
  39. float bodyOffset,
  40. float bottom)
  41. {
  42. contentRoot = content;
  43. expandable = canExpand;
  44. headerHeight = header;
  45. bodyTop = bodyOffset;
  46. bottomPadding = bottom;
  47. CacheReferences();
  48. if (headerButton != null)
  49. headerButton.gameObject.SetActive(canExpand);
  50. if (arrow != null)
  51. arrow.gameObject.SetActive(canExpand);
  52. SetExpanded(canExpand ? openByDefault : true, false);
  53. }
  54. public void SetData(string title, string body)
  55. {
  56. CacheReferences();
  57. if (titleText != null)
  58. titleText.text = title;
  59. if (bodyText != null)
  60. bodyText.text = body;
  61. ApplyHeight(false);
  62. }
  63. public void SetNumber(Sprite sprite)
  64. {
  65. CacheReferences();
  66. if (numberImage != null && sprite != null)
  67. numberImage.sprite = sprite;
  68. }
  69. public void RefreshLayout()
  70. {
  71. ApplyHeight(false);
  72. }
  73. public void Toggle()
  74. {
  75. if (!expandable)
  76. return;
  77. SetExpanded(!expanded, false);
  78. }
  79. public void SetExpanded(bool value, bool immediate)
  80. {
  81. if (!expandable)
  82. value = true;
  83. expanded = value;
  84. if (bodyText != null)
  85. bodyText.gameObject.SetActive(value);
  86. if (bodyRect != null)
  87. bodyRect.gameObject.SetActive(value);
  88. if (divider != null)
  89. divider.SetActive(value);
  90. if (arrow != null)
  91. arrow.localRotation = Quaternion.Euler(0f, 0f, value ? 180f : 0f);
  92. ApplyHeight(immediate);
  93. }
  94. void ApplyHeight(bool immediate)
  95. {
  96. if (layoutElement == null)
  97. layoutElement = GetComponent<LayoutElement>();
  98. if (layoutElement == null)
  99. return;
  100. layoutElement.flexibleHeight = 0f;
  101. if (!expanded)
  102. {
  103. layoutElement.preferredHeight = headerHeight;
  104. }
  105. else
  106. {
  107. float textHeight = MeasureBodyHeight();
  108. if (bodyRect != null)
  109. bodyRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, textHeight);
  110. layoutElement.preferredHeight = bodyTop + textHeight + bottomPadding;
  111. }
  112. MarkLayout(immediate);
  113. }
  114. float MeasureBodyHeight()
  115. {
  116. if (bodyText == null || bodyRect == null)
  117. return 0f;
  118. Canvas.ForceUpdateCanvases();
  119. float width = bodyRect.rect.width;
  120. if (width <= 1f)
  121. {
  122. bool fixedWidth = Mathf.Approximately(bodyRect.anchorMin.x, bodyRect.anchorMax.x);
  123. if (fixedWidth)
  124. {
  125. width = bodyRect.sizeDelta.x;
  126. }
  127. else
  128. {
  129. float parentWidth = ((RectTransform)transform).rect.width;
  130. if (parentWidth <= 1f && contentRoot != null)
  131. parentWidth = contentRoot.rect.width;
  132. width = parentWidth + bodyRect.sizeDelta.x;
  133. }
  134. }
  135. width = Mathf.Max(1f, width);
  136. return bodyText.GetPreferredValues(bodyText.text, width, Mathf.Infinity).y;
  137. }
  138. void MarkLayout(bool immediate)
  139. {
  140. if (contentRoot == null)
  141. return;
  142. if (immediate)
  143. LayoutRebuilder.ForceRebuildLayoutImmediate(contentRoot);
  144. else
  145. LayoutRebuilder.MarkLayoutForRebuild(contentRoot);
  146. }
  147. void CacheReferences()
  148. {
  149. if (layoutElement == null)
  150. layoutElement = GetComponent<LayoutElement>();
  151. if (titleText == null)
  152. {
  153. Transform title = transform.Find("IMG_BG/TMP_Title");
  154. if (title == null)
  155. title = transform.Find("TMP_Title");
  156. if (title != null)
  157. titleText = title.GetComponent<TMP_Text>();
  158. }
  159. if (bodyText == null)
  160. {
  161. Transform body = transform.Find("IMG_BG/TMP_Content");
  162. if (body == null)
  163. body = transform.Find("TMP_Content");
  164. if (body != null)
  165. bodyText = body.GetComponent<TMP_Text>();
  166. }
  167. if (bodyRect == null && bodyText != null)
  168. bodyRect = bodyText.rectTransform;
  169. if (headerButton == null)
  170. {
  171. Transform header = transform.Find("BTN_FAQToggle");
  172. if (header != null)
  173. headerButton = header.GetComponent<Button>();
  174. }
  175. if (arrow == null)
  176. arrow = transform.Find("BTN_FAQToggle/IMG_Arrow") as RectTransform;
  177. if (divider == null)
  178. {
  179. Transform dividerTransform = transform.Find("IMG_BG/Image");
  180. if (dividerTransform != null)
  181. divider = dividerTransform.gameObject;
  182. }
  183. if (numberImage == null)
  184. {
  185. Transform number = transform.Find("IMG_Num");
  186. if (number != null)
  187. numberImage = number.GetComponent<Image>();
  188. }
  189. }
  190. }