using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; [RequireComponent(typeof(Button))] public class customDropDown : MonoBehaviour { public Text label; public GameObject dropmenu; public GameObject template; public GameObject contentParent; [SerializeField] private List options; public UnityEvent OnChangedValue; public int selectedIndex; public void SetValue(int value){ selectedIndex = value; RefreshMenu(); } public void SetOptions(List _options){ options = _options; RefreshMenu(); } public void RemoveOption(string item){ options.Remove(item); RefreshMenu(); } public void AddOption(string item){ options.Add(item); RefreshMenu(); } public List GetOptions(){ return options; } public int GetOption(string value){ for(int i =0; i < options.Count; i++){ if(options[i] == value){return i;} } return 0; } public void toggleDropmenu(){ if(!dropmenu.activeSelf){ RefreshMenu(); } dropmenu.SetActive(!dropmenu.activeSelf); } List items; public void RefreshMenu(){ //clean the list before adding new items Transform[] children = contentParent.transform.GetComponentsInChildren(); foreach(Transform child in children){ if(child != contentParent.transform && child != template.transform){ Destroy(child.gameObject); } } if(items==null){items = new List();} items.Clear(); //all clear; let's go for(int i =0; i < options.Count; i++){ string option = options[i]; GameObject item = Instantiate(template, contentParent.transform); try{ item.SetActive(true); item.transform.GetChild(0).GetComponent().enabled=i == selectedIndex; item.transform.GetChild(1).GetComponent().text = option; }catch{} int curIndex =i; item.GetComponent