using System.Collections; using System.Reflection; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using TMPro; public class CustomTMPDropdown : TMP_Dropdown { private FieldInfo dropdownField; private FieldInfo blockerField; private GameObject dropdownListGO; private int currentOptionIndex = 0; // 光标在 options 索引 protected override void Awake() { base.Awake(); dropdownField = typeof(TMP_Dropdown).GetField( "m_Dropdown", BindingFlags.NonPublic | BindingFlags.Instance ); blockerField = typeof(TMP_Dropdown).GetField( "m_Blocker", BindingFlags.NonPublic | BindingFlags.Instance ); } public override void OnPointerClick(PointerEventData eventData) { } public override void OnSubmit(BaseEventData eventData) { } public override void OnCancel(BaseEventData eventData) { } private GameObject GetDropdownGO() => dropdownField?.GetValue(this) as GameObject; private GameObject GetBlockerGO() => blockerField?.GetValue(this) as GameObject; // ---------------- 手动打开/关闭 ---------------- public void OpenDropdown() { if (dropdownListGO != null && dropdownListGO.activeSelf) return; base.Show(); StartCoroutine(FixAfterShow()); } public void CloseDropdown() { if (dropdownListGO != null) Destroy(dropdownListGO); GameObject blocker = GetBlockerGO(); if (blocker != null) Destroy(blocker); dropdownListGO = null; } public void ToggleDropdown() { var list = GetDropdownGO(); if (list != null && list.activeSelf) CloseDropdown(); else OpenDropdown(); } // ---------------- 上下切换 ---------------- public void SelectNext() { if (options.Count == 0) return; currentOptionIndex = (currentOptionIndex + 1) % options.Count; value = currentOptionIndex; RefreshShownValue(); UpdateHighlight(); } public void SelectPrevious() { if (options.Count == 0) return; currentOptionIndex = (currentOptionIndex - 1 + options.Count) % options.Count; value = currentOptionIndex; RefreshShownValue(); UpdateHighlight(); } // ---------------- 提交 ---------------- public void SubmitSelection() { value = currentOptionIndex; RefreshShownValue(); onValueChanged?.Invoke(value); CloseDropdown(); } // ---------------- 初始化 Dropdown ---------------- private IEnumerator FixAfterShow() { yield return null; // 等一帧 dropdownListGO = GetDropdownGO(); if (dropdownListGO == null) yield break; // 禁用 Blocker 射线拦截 GameObject blocker = GetBlockerGO(); if (blocker != null) { Button btn = blocker.GetComponent