using AZTECHWB.Extensions; using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; namespace AZTECHWB.UI { public class GraphicsSettingItem : MonoBehaviour { TMP_Dropdown Dropdown_Value; Toggle Toggle_Value; bool isDropdownOpen; Image dropdownImage; Sprite dropdownNormalSprite; Sprite dropdownSelectedSprite; public void InitDropdown(UnityAction onValueChanged) { transform.TryGetComponentInChildren(nameof(Dropdown_Value), out Dropdown_Value); Dropdown_Value.onValueChanged.AddListener(onValueChanged); dropdownImage = Dropdown_Value.targetGraphic as Image; dropdownNormalSprite = dropdownImage.sprite; dropdownSelectedSprite = Dropdown_Value.spriteState.selectedSprite; } public void InitToggle(UnityAction onValueChanged) { transform.TryGetComponentInChildren(nameof(Toggle_Value), out Toggle_Value); Toggle_Value.onValueChanged.AddListener(onValueChanged); } public void SetDropdownOptions(string[] options) { Dropdown_Value.ClearOptions(); Dropdown_Value.AddOptions(new System.Collections.Generic.List(options)); } public void SetDropdownValue(int index) { Dropdown_Value.SetValueWithoutNotify(index); } public void SetToggleValue(bool value) { Toggle_Value.SetIsOnWithoutNotify(value); } void LateUpdate() { if (Dropdown_Value == null) return; var list = Dropdown_Value.transform.Find("Dropdown List"); if (list != null && !isDropdownOpen) { isDropdownOpen = true; StyleDropdownItems(list); } if (isDropdownOpen) { if (list != null) { dropdownImage.sprite = dropdownSelectedSprite; } else { dropdownImage.sprite = dropdownNormalSprite; isDropdownOpen = false; var eventSystem = UnityEngine.EventSystems.EventSystem.current; if (eventSystem != null) eventSystem.SetSelectedGameObject(null); } } } void StyleDropdownItems(Transform list) { var content = list.Find("Viewport/Content"); if (content == null) return; for (int i = 0; i < content.childCount; i++) { var item = content.GetChild(i); if (item.GetComponent() == null) { var styler = item.gameObject.AddComponent(); styler.Init(); } } } } }