ColumnMenuUI.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.UI.Tooltips;
  8. using KairoEngine.UI.InteractionHandler;
  9. namespace KairoEngine.UI
  10. {
  11. [HideMonoScript]
  12. public class ColumnMenuUI : MenuUI
  13. {
  14. [BoxGroup("Settings"), OnInspectorInit("EditorSetup")] public GameObject subMenuPrefab;
  15. [BoxGroup("Settings")] public GameObject subMenuButtonPrefab;
  16. [BoxGroup("Settings")] public RectTransform subMenusParent;
  17. [BoxGroup("Settings")] public bool instantiateMainMenuGroup = true;
  18. [BoxGroup("Settings")] public bool keepMenuButtonsSelected = false;
  19. [BoxGroup("Settings")] public bool keepSubMenuButtonsSelected = false;
  20. private GameObject mainMenu;
  21. private List<GameObject> subMenus = new List<GameObject>();
  22. private List<SelectedButton> selectButtons = new List<SelectedButton>();
  23. public override void Start()
  24. {
  25. useSubMenus = true;
  26. if(handler == null) handler = gameObject.GetComponents<IClickHandler>()[1];
  27. if(createMenuOnStart)
  28. {
  29. DestroyMenu();
  30. CreateMenu();
  31. }
  32. }
  33. public override void OnClick(string title)
  34. {
  35. for (int i = 0; i < buttons.Count; i++)
  36. {
  37. if(buttons[i].title == title)
  38. {
  39. if(buttons[i].subMenuParent)
  40. {
  41. OpenSubMenu(title);
  42. SelectParentButton(i);
  43. }
  44. if(buttons[i].action != "" && handler != null)
  45. {
  46. SelectChildButton(i);
  47. handler.OnClick(buttons[i].action);
  48. }
  49. return;
  50. }
  51. }
  52. }
  53. public override void CreateMenu() => CreateMenus();
  54. public void CreateMenus()
  55. {
  56. if(instantiateMainMenuGroup) mainMenu = Instantiate(subMenuPrefab, menuParent);
  57. else mainMenu = menuParent.gameObject;
  58. for (int i = 0; i < buttons.Count; i++)
  59. {
  60. if(string.IsNullOrEmpty(buttons[i].parent))
  61. {
  62. var btn = CreateButton(buttons[i], mainMenu.transform, buttonPrefab);
  63. selectButtons.Add(btn.GetComponent<SelectedButton>());
  64. buttons[i].button = btn;
  65. var subMenu = Instantiate(subMenuPrefab, subMenusParent);
  66. subMenu.name = $"SubMenu {buttons[i].title}";
  67. for (int a = 0; a < buttons.Count; a++)
  68. {
  69. if(buttons[a].parent == buttons[i].title)
  70. {
  71. var subBtn = CreateButton(buttons[a], subMenu.transform, subMenuButtonPrefab ?? buttonPrefab);
  72. selectButtons.Add(subBtn.GetComponent<SelectedButton>());
  73. buttons[a].button = subBtn;
  74. }
  75. }
  76. subMenu.SetActive(false);
  77. subMenus.Add(subMenu);
  78. }
  79. }
  80. }
  81. public override void DestroyMenu() => DestroyMenus();
  82. public void DestroyMenus()
  83. {
  84. for (int i = 0; i < subMenus.Count; i++)
  85. {
  86. #if UNITY_EDITOR
  87. DestroyImmediate(subMenus[i]);
  88. #else
  89. Destroy(subMenus[i]);
  90. #endif
  91. }
  92. subMenus = new List<GameObject>();
  93. #if UNITY_EDITOR
  94. if(instantiateMainMenuGroup) DestroyImmediate(mainMenu);
  95. else foreach (var btn in buttons) DestroyImmediate(btn.button.gameObject);
  96. #else
  97. if(instantiateMainMenuGroup) Destroy(mainMenu);
  98. else foreach (var btn in buttons) Destroy(btn.button.gameObject);
  99. #endif
  100. buttons = new List<MenuButtomData>();
  101. }
  102. public void OpenSubMenu(string title)
  103. {
  104. for (int i = 0; i < subMenus.Count; i++)
  105. {
  106. if(subMenus[i] == null) continue;
  107. if($"SubMenu {title}" == subMenus[i].name) subMenus[i].SetActive(true);
  108. else subMenus[i].SetActive(false);
  109. }
  110. }
  111. public void CloseSubMenus()
  112. {
  113. for (int i = 0; i < subMenus.Count; i++)
  114. {
  115. if(subMenus[i] != null) subMenus[i].SetActive(false);
  116. }
  117. }
  118. public void EditorSetup()
  119. {
  120. useSubMenus = true;
  121. }
  122. public void SelectParentButton(int index)
  123. {
  124. for (int i = 0; i < buttons.Count; i++)
  125. {
  126. if(i != index && buttons[i].subMenuParent) selectButtons[i].Deselect();
  127. else if(i == index) selectButtons[i].Select();
  128. }
  129. }
  130. public void SelectChildButton(int index)
  131. {
  132. for (int i = 0; i < buttons.Count; i++)
  133. {
  134. if(i != index && !buttons[i].subMenuParent) selectButtons[i].Deselect();
  135. else if(i == index) selectButtons[i].Select();
  136. }
  137. }
  138. public void DeselectChildButtons()
  139. {
  140. for (int i = 0; i < buttons.Count; i++)
  141. {
  142. if(!buttons[i].subMenuParent) selectButtons[i].Deselect();
  143. }
  144. }
  145. public void DeselectAllButtons()
  146. {
  147. for (int i = 0; i < buttons.Count; i++) selectButtons[i].Deselect();
  148. }
  149. }
  150. }