customDropDown.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.UI;
  6. [RequireComponent(typeof(Button))]
  7. public class customDropDown : MonoBehaviour
  8. {
  9. public Text label;
  10. public GameObject dropmenu;
  11. public GameObject template;
  12. public GameObject contentParent;
  13. [SerializeField]
  14. private List<string> options;
  15. public UnityEvent OnChangedValue;
  16. public int selectedIndex;
  17. public void SetValue(int value){
  18. selectedIndex = value;
  19. RefreshMenu();
  20. }
  21. public void SetOptions(List<string> _options){
  22. options = _options;
  23. RefreshMenu();
  24. }
  25. public void RemoveOption(string item){
  26. options.Remove(item);
  27. RefreshMenu();
  28. }
  29. public void AddOption(string item){
  30. options.Add(item);
  31. RefreshMenu();
  32. }
  33. public List<string> GetOptions(){
  34. return options;
  35. }
  36. public int GetOption(string value){
  37. for(int i =0; i < options.Count; i++){
  38. if(options[i] == value){return i;}
  39. }
  40. return 0;
  41. }
  42. public void toggleDropmenu(){
  43. if(!dropmenu.activeSelf){
  44. RefreshMenu();
  45. }
  46. dropmenu.SetActive(!dropmenu.activeSelf);
  47. }
  48. List<GameObject> items;
  49. public void RefreshMenu(){
  50. //clean the list before adding new items
  51. Transform[] children = contentParent.transform.GetComponentsInChildren<Transform>();
  52. foreach(Transform child in children){
  53. if(child != contentParent.transform && child != template.transform){
  54. Destroy(child.gameObject);
  55. }
  56. }
  57. if(items==null){items = new List<GameObject>();}
  58. items.Clear();
  59. //all clear; let's go
  60. for(int i =0; i < options.Count; i++){
  61. string option = options[i];
  62. GameObject item = Instantiate(template, contentParent.transform);
  63. try{
  64. item.SetActive(true);
  65. item.transform.GetChild(0).GetComponent<Image>().enabled=i == selectedIndex;
  66. item.transform.GetChild(1).GetComponent<Text>().text = option;
  67. }catch{}
  68. int curIndex =i;
  69. item.GetComponent<Button>().onClick.AddListener(()=>{_onSelectedOption(curIndex);});
  70. items.Add(item);
  71. if(selectedIndex ==i){
  72. label.text = option;
  73. }
  74. }
  75. label.text = options[selectedIndex];
  76. }
  77. private void _onSelectedOption(int id){
  78. selectedIndex=id;
  79. for(int i =0; i < items.Count; i++){
  80. if(i != id){
  81. items[i].transform.GetChild(0).GetComponent<Image>().enabled=false;
  82. }else{
  83. items[i].transform.GetChild(0).GetComponent<Image>().enabled = true;
  84. }
  85. }
  86. dropmenu.SetActive(false);
  87. label.text = options[id];
  88. OnChangedValue.Invoke();
  89. }
  90. void Start()
  91. {
  92. GetComponent<Button>().onClick.AddListener(toggleDropmenu);
  93. template.SetActive(false);
  94. RefreshMenu();
  95. dropmenu.SetActive(false);
  96. }
  97. }