MenuUI.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using Sirenix.OdinInspector;
  6. using UnityEngine.UI;
  7. using KairoEngine.UI.Tooltips;
  8. using KairoEngine.UI.InteractionHandler;
  9. namespace KairoEngine.UI
  10. {
  11. [HideMonoScript]
  12. public class MenuUI : MonoBehaviour, IClickHandler, IUiSystemElement
  13. {
  14. [BoxGroup("Settings")] public string elementTitle = "New UI Element";
  15. [BoxGroup("Settings")] public bool createMenuOnStart = true;
  16. [BoxGroup("Settings")] public bool visibleOnStart = true;
  17. [BoxGroup("Settings")] public GameObject buttonPrefab;
  18. [BoxGroup("Settings")] public RectTransform menuParent;
  19. [BoxGroup("Settings")] public Canvas menuCanvas;
  20. [PropertySpace(4,4), OnCollectionChanged("UpdateButtonData")] public List<MenuButtomData> buttons = new List<MenuButtomData>();
  21. private bool isVisible = false;
  22. [HideInInspector] public IClickHandler handler;
  23. [HideInInspector] public bool useSubMenus = false;
  24. public virtual void Start()
  25. {
  26. if(handler == null) handler = gameObject.GetComponent<IClickHandler>();
  27. if(createMenuOnStart)
  28. {
  29. DestroyMenu();
  30. CreateMenu();
  31. }
  32. if(visibleOnStart) UiManager.ShowElement(this);
  33. else UiManager.HideElement(this);
  34. }
  35. private void OnDisable()
  36. {
  37. UiManager.UnregisterElement(this);
  38. }
  39. private void OnEnable()
  40. {
  41. UiManager.RegisterElement(elementTitle, this, this.transform, isVisible);
  42. }
  43. public virtual void OnClick(string title)
  44. {
  45. for (int i = 0; i < buttons.Count; i++)
  46. {
  47. if(buttons[i].title == title)
  48. {
  49. if(buttons[i].action != "" && handler != null)
  50. {
  51. handler.OnClick(buttons[i].action);
  52. }
  53. }
  54. }
  55. }
  56. public void Hide()
  57. {
  58. menuCanvas.gameObject.SetActive(false);
  59. isVisible = false;
  60. }
  61. public void Show()
  62. {
  63. menuCanvas.gameObject.SetActive(true);
  64. isVisible = true;
  65. }
  66. public bool IsVisible() => isVisible;
  67. public virtual GameObject CreateButton(MenuButtomData data, Transform parent)
  68. {
  69. var btn = Instantiate(buttonPrefab, parent);
  70. ClickHandler clickHandler = btn.GetComponent<ClickHandler>();
  71. if(clickHandler != null)
  72. {
  73. clickHandler.receiver = this;
  74. clickHandler.title = data.title;
  75. }
  76. ButtonData buttonData = btn.GetComponent<ButtonData>();
  77. if(buttonData != null) buttonData.Setup(data.title, data.graphic);
  78. TooltipTrigger tooltip = btn.GetComponent<TooltipTrigger>();
  79. if(tooltip != null)
  80. {
  81. if(data.showTooltip)
  82. {
  83. tooltip.header = data.title;
  84. tooltip.content = data.description;
  85. tooltip.tooltipType = data.tooltipType;
  86. }
  87. else tooltip.enabled = false;
  88. }
  89. Button buttonComponent = btn.GetComponent<Button>();
  90. if(buttonComponent != null) buttonComponent.interactable = data.isInteractable;
  91. return btn;
  92. }
  93. [ButtonGroup("Buttons"), Button("Create Menu")]
  94. public virtual void CreateMenu()
  95. {
  96. for (int i = 0; i < buttons.Count; i++)
  97. {
  98. var btn = CreateButton(buttons[i], menuParent);
  99. buttons[i].button = btn;
  100. }
  101. }
  102. [ButtonGroup("Buttons"), Button("Destroy Menu")]
  103. public virtual void DestroyMenu()
  104. {
  105. for (int i = 0; i < buttons.Count; i++)
  106. {
  107. #if UNITY_EDITOR
  108. DestroyImmediate(buttons[i].button);
  109. #else
  110. Destroy(buttons[i].button);
  111. #endif
  112. }
  113. }
  114. private void UpdateButtonData()
  115. {
  116. for (int i = 0; i < buttons.Count; i++)
  117. {
  118. buttons[i].useSubMenus = useSubMenus;
  119. }
  120. }
  121. }
  122. }