CheckboxConfigOptionUI.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 CheckboxConfigOptionUI : MonoBehaviour
  14. {
  15. public TextMeshProUGUI titleText;
  16. public TooltipTrigger tooltipTrigger;
  17. public Toggle checkbox;
  18. [ReadOnly] public ConfigOptionBase configOption;
  19. public void OnEnable()
  20. {
  21. checkbox.onValueChanged.AddListener(delegate {OnChange(); });
  22. }
  23. void OnDisable()
  24. {
  25. checkbox.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. checkbox.isOn = uiData.checkboxData.currentValue;
  35. }
  36. public void OnChange()
  37. {
  38. ApplyConfig();
  39. }
  40. private void ApplyConfig()
  41. {
  42. if(configOption == null) return;
  43. ConfigOptionData data = new ConfigOptionData("", "", checkbox.isOn ? 1 : 0, 0f);
  44. configOption.SetValue(data, false);
  45. }
  46. private void SaveConfig()
  47. {
  48. if(configOption == null) return;
  49. ConfigOptionData data = new ConfigOptionData("", "", checkbox.isOn ? 1 : 0, 0f);
  50. configOption.SetValue(data, true);
  51. }
  52. }
  53. }