using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; public class ComboSelection : MonoBehaviour { public string[] Options; public string SelectedOption; public int SelectedIndex =0; [SerializeField]private TMP_Text selectedText; [SerializeField]private Button btnNext; [SerializeField]private Button btnPrev; public UnityEvent OnSelectedChanged; CanvasGroup canvasGroup; void OnValidate(){ if(selectedText==null) selectedText = GetComponentInChildren(); } void Awake(){ canvasGroup = GetComponent(); btnNext.onClick.AddListener(OnNext); btnPrev.onClick.AddListener(OnPrev); Refresh(); } public void SetInteractable(bool val){ canvasGroup.interactable = val; } void OnNext(){ if(SelectedIndex < Options.Length-1){ SelectedIndex++; }else{ SelectedIndex = 0; } Refresh(); } void OnPrev(){ if(SelectedIndex > 0){ SelectedIndex--; }else{ SelectedIndex = Options.Length-1; } Refresh(); } public void SetValue(string newVal){ for(int i=0; i < Options.Length; i++){ if(Options[i].ToLower() == newVal.ToLower()){ SelectedIndex = i; Refresh(); return; } } Debug.LogError($"Cant set value to an non-existing option ({newVal})"); } public void Refresh(){ selectedText.text = Options[SelectedIndex]; SelectedOption = Options[SelectedIndex]; OnSelectedChanged.Invoke(); } }