using TMPro; using UnityEngine; using UnityEngine.UI; [DisallowMultipleComponent] [RequireComponent(typeof(LayoutElement))] public class UIHelpItem : MonoBehaviour { [Header("Layout")] [SerializeField] float headerHeight = 60f; [SerializeField] float bodyTop = 68f; [SerializeField] float bottomPadding = 16f; RectTransform contentRoot; RectTransform bodyRect; RectTransform arrow; TMP_Text titleText; TMP_Text bodyText; Button headerButton; Image numberImage; GameObject divider; LayoutElement layoutElement; bool expandable; bool expanded; public Button HeaderButton => headerButton; public bool Expandable => expandable; public bool Expanded => expanded; void Awake() { layoutElement = GetComponent(); if (layoutElement == null) layoutElement = gameObject.AddComponent(); layoutElement.flexibleHeight = 0f; CacheReferences(); } public void Configure( RectTransform content, bool canExpand, bool openByDefault, float header, float bodyOffset, float bottom) { contentRoot = content; expandable = canExpand; headerHeight = header; bodyTop = bodyOffset; bottomPadding = bottom; CacheReferences(); if (headerButton != null) headerButton.gameObject.SetActive(canExpand); if (arrow != null) arrow.gameObject.SetActive(canExpand); SetExpanded(canExpand ? openByDefault : true, false); } public void SetData(string title, string body) { CacheReferences(); if (titleText != null) titleText.text = title; if (bodyText != null) bodyText.text = body; ApplyHeight(false); } public void SetNumber(Sprite sprite) { CacheReferences(); if (numberImage != null && sprite != null) numberImage.sprite = sprite; } public void RefreshLayout() { ApplyHeight(false); } public void Toggle() { if (!expandable) return; SetExpanded(!expanded, false); } public void SetExpanded(bool value, bool immediate) { if (!expandable) value = true; expanded = value; if (bodyText != null) bodyText.gameObject.SetActive(value); if (bodyRect != null) bodyRect.gameObject.SetActive(value); if (divider != null) divider.SetActive(value); if (arrow != null) arrow.localRotation = Quaternion.Euler(0f, 0f, value ? 180f : 0f); ApplyHeight(immediate); } void ApplyHeight(bool immediate) { if (layoutElement == null) layoutElement = GetComponent(); if (layoutElement == null) return; layoutElement.flexibleHeight = 0f; if (!expanded) { layoutElement.preferredHeight = headerHeight; } else { float textHeight = MeasureBodyHeight(); if (bodyRect != null) bodyRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, textHeight); layoutElement.preferredHeight = bodyTop + textHeight + bottomPadding; } MarkLayout(immediate); } float MeasureBodyHeight() { if (bodyText == null || bodyRect == null) return 0f; Canvas.ForceUpdateCanvases(); float width = bodyRect.rect.width; if (width <= 1f) { bool fixedWidth = Mathf.Approximately(bodyRect.anchorMin.x, bodyRect.anchorMax.x); if (fixedWidth) { width = bodyRect.sizeDelta.x; } else { float parentWidth = ((RectTransform)transform).rect.width; if (parentWidth <= 1f && contentRoot != null) parentWidth = contentRoot.rect.width; width = parentWidth + bodyRect.sizeDelta.x; } } width = Mathf.Max(1f, width); return bodyText.GetPreferredValues(bodyText.text, width, Mathf.Infinity).y; } void MarkLayout(bool immediate) { if (contentRoot == null) return; if (immediate) LayoutRebuilder.ForceRebuildLayoutImmediate(contentRoot); else LayoutRebuilder.MarkLayoutForRebuild(contentRoot); } void CacheReferences() { if (layoutElement == null) layoutElement = GetComponent(); if (titleText == null) { Transform title = transform.Find("IMG_BG/TMP_Title"); if (title == null) title = transform.Find("TMP_Title"); if (title != null) titleText = title.GetComponent(); } if (bodyText == null) { Transform body = transform.Find("IMG_BG/TMP_Content"); if (body == null) body = transform.Find("TMP_Content"); if (body != null) bodyText = body.GetComponent(); } if (bodyRect == null && bodyText != null) bodyRect = bodyText.rectTransform; if (headerButton == null) { Transform header = transform.Find("BTN_FAQToggle"); if (header != null) headerButton = header.GetComponent