using System.Collections; using System.Reflection; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using TMPro; [RequireComponent(typeof(TMP_Dropdown))] public class CustomTMPDropdown : TMP_Dropdown { private FieldInfo dropdownField; private FieldInfo blockerField; private GameObject dropdownListGO; private int currentOptionIndex = 0; 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); // 添加Outline Outline ol = gameObject.GetComponent(); if (ol == null) ol = gameObject.AddComponent(); ol.effectColor = new Color32(0xFF, 0xAB, 0x00, 255); ol.effectDistance = new Vector2(2, -2); ol.enabled = false; } public override void OnPointerClick(PointerEventData eventData) { if (FocusManager.Instance.currentPage != null) { FocusManager.Instance.currentPage.UpdateFouceByClick(this); } ToggleDropdown(); } public override void OnSubmit(BaseEventData eventData) { SubmitSelection(); } 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() { if (dropdownListGO != null && dropdownListGO.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() { if (dropdownListGO == null || !dropdownListGO.activeSelf) { OpenDropdown(); } else { 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