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(); } // 当点击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()); } } }