ConfigOptionsUI.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. using KairoEngine.Core.ConfigOptions;
  6. using Sirenix.OdinInspector;
  7. namespace KairoEngine.UI.ConfigOptions
  8. {
  9. [HideMonoScript]
  10. public class ConfigOptionsUI : MonoBehaviour
  11. {
  12. [LabelText("Config Options")] public List<ConfigOptionBase> list = new List<ConfigOptionBase>();
  13. public GameObject sliderPrefab;
  14. public GameObject checkboxPrefab;
  15. public GameObject dropdownPrefab;
  16. public Transform container;
  17. private List<GameObject> configOptionsGameObjects;
  18. private void Awake()
  19. {
  20. if(sliderPrefab == null) Debug.LogError("sliderPrefab is missing in ConfigOptionsUi.", this.gameObject);
  21. if(checkboxPrefab == null) Debug.LogError("checkboxPrefab is missing in ConfigOptionsUi.", this.gameObject);
  22. if(dropdownPrefab == null) Debug.LogError("dropdownPrefab is missing in ConfigOptionsUi.", this.gameObject);
  23. if(container == null) Debug.LogError("container is missing in ConfigOptionsUi.", this.gameObject);
  24. }
  25. private void Start()
  26. {
  27. Timer.Execute(2000f, () => {
  28. GenerateConfigMenu();
  29. });
  30. }
  31. public void GenerateConfigMenu()
  32. {
  33. if(configOptionsGameObjects != null)
  34. {
  35. for (int i = 0; i < configOptionsGameObjects.Count; i++)
  36. {
  37. Destroy(configOptionsGameObjects[i]);
  38. }
  39. }
  40. configOptionsGameObjects = new List<GameObject>();
  41. for (int i = 0; i < list.Count; i++)
  42. {
  43. GameObject obj = null;
  44. switch(list[i].uiElementType)
  45. {
  46. case ConfigOptionUIType.Slider:
  47. obj = Instantiate(sliderPrefab, container);
  48. obj.GetComponent<SliderConfigOptionUI>().Setup(list[i]);
  49. break;
  50. case ConfigOptionUIType.Checkbox:
  51. obj = Instantiate(checkboxPrefab, container);
  52. obj.GetComponent<CheckboxConfigOptionUI>().Setup(list[i]);
  53. break;
  54. case ConfigOptionUIType.Dropdown:
  55. obj = Instantiate(dropdownPrefab, container);
  56. obj.GetComponent<DropdownConfigOptionUI>().Setup(list[i]);
  57. break;
  58. default:
  59. break;
  60. }
  61. if(obj != null) configOptionsGameObjects.Add(obj);
  62. }
  63. }
  64. }
  65. }