DropdownConfigOptionUI.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using KairoEngine.Core;
  6. using KairoEngine.Core.ConfigOptions;
  7. using KairoEngine.UI.Tooltips;
  8. using Sirenix.OdinInspector;
  9. using TMPro;
  10. namespace KairoEngine.UI.ConfigOptions
  11. {
  12. [HideMonoScript]
  13. public class DropdownConfigOptionUI : MonoBehaviour
  14. {
  15. public TextMeshProUGUI titleText;
  16. public TooltipTrigger tooltipTrigger;
  17. public TMP_Dropdown dropdown;
  18. [ReadOnly] public ConfigOptionBase configOption;
  19. public void OnEnable()
  20. {
  21. dropdown.onValueChanged.AddListener(delegate {OnChange(); });
  22. }
  23. void OnDisable()
  24. {
  25. dropdown.onValueChanged.RemoveListener(delegate {OnChange(); });
  26. }
  27. public void Setup(ConfigOptionBase configOption)
  28. {
  29. this.configOption = configOption;
  30. var uiData = configOption.GetUiData();
  31. titleText.text = uiData.name;
  32. //tooltipTrigger.header = uiData.name;
  33. tooltipTrigger.content = uiData.description;
  34. var options = new List<TMP_Dropdown.OptionData>();
  35. for (int i = 0; i < uiData.dropdownData.options.Count; i++)
  36. {
  37. options.Add(new TMP_Dropdown.OptionData( uiData.dropdownData.options[i]));
  38. }
  39. dropdown.AddOptions(options);
  40. dropdown.value = uiData.dropdownData.currentValue;
  41. }
  42. public void OnChange()
  43. {
  44. ApplyConfig();
  45. }
  46. private void ApplyConfig()
  47. {
  48. if(configOption == null) return;
  49. ConfigOptionData data = new ConfigOptionData("", "", dropdown.value, 0f);
  50. configOption.SetValue(data, false);
  51. }
  52. private void SaveConfig()
  53. {
  54. if(configOption == null) return;
  55. ConfigOptionData data = new ConfigOptionData("", "", dropdown.value, 0f);
  56. configOption.SetValue(data, true);
  57. }
  58. }
  59. }