| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- 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<LayoutElement>();
- if (layoutElement == null)
- layoutElement = gameObject.AddComponent<LayoutElement>();
- 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<LayoutElement>();
- 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<LayoutElement>();
- 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<TMP_Text>();
- }
- 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<TMP_Text>();
- }
- if (bodyRect == null && bodyText != null)
- bodyRect = bodyText.rectTransform;
- if (headerButton == null)
- {
- Transform header = transform.Find("BTN_FAQToggle");
- if (header != null)
- headerButton = header.GetComponent<Button>();
- }
- if (arrow == null)
- arrow = transform.Find("BTN_FAQToggle/IMG_Arrow") as RectTransform;
- if (divider == null)
- {
- Transform dividerTransform = transform.Find("IMG_BG/Image");
- if (dividerTransform != null)
- divider = dividerTransform.gameObject;
- }
- if (numberImage == null)
- {
- Transform number = transform.Find("IMG_Num");
- if (number != null)
- numberImage = number.GetComponent<Image>();
- }
- }
- }
|