TH1/Unity/Assets/Scripts/TH1_UI/Utilities/DropdownRebuildTrigger.cs
2026-01-09 03:41:55 +08:00

37 lines
1.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;
public class DropdownRebuildTrigger : MonoBehaviour, IPointerClickHandler
{
private TMP_Dropdown _dropdown;
void Awake()
{
_dropdown = GetComponent<TMP_Dropdown>();
}
// 当点击Dropdown时触发
public void OnPointerClick(PointerEventData eventData)
{
// 只有当Dropdown是可以交互的才处理
if (_dropdown != null && _dropdown.interactable)
{
StartCoroutine(RebuildLayoutNextFrame());
}
}
IEnumerator RebuildLayoutNextFrame()
{
// 等待当前帧结束等待Dropdown内部逻辑完成列表的Instantiate
yield return new WaitForEndOfFrame();
// 只有当列表确实处于展开状态时,才刷新
if (_dropdown.IsExpanded)
{
LayoutRebuilder.ForceRebuildLayoutImmediate(_dropdown.transform.Find("Dropdown List/Viewport").gameObject.GetComponent<RectTransform>());
}
}
}