ColumnMenuUI.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 RectTransform subMenusParent;
  16. private GameObject mainMenu;
  17. private List<GameObject> subMenus = new List<GameObject>();
  18. public override void Start()
  19. {
  20. useSubMenus = true;
  21. if(handler == null) handler = gameObject.GetComponent<IClickHandler>();
  22. if(createMenuOnStart)
  23. {
  24. DestroyMenu();
  25. CreateMenu();
  26. }
  27. }
  28. public override void OnClick(string title)
  29. {
  30. for (int i = 0; i < buttons.Count; i++)
  31. {
  32. if(buttons[i].title == title)
  33. {
  34. if(buttons[i].subMenuParent) OpenSubMenu(title);
  35. if(buttons[i].action != "" && handler != null)
  36. {
  37. handler.OnClick(buttons[i].action);
  38. }
  39. }
  40. }
  41. }
  42. public override void CreateMenu() => CreateMenus();
  43. public void CreateMenus()
  44. {
  45. mainMenu = Instantiate(subMenuPrefab, menuParent);
  46. for (int i = 0; i < buttons.Count; i++)
  47. {
  48. if(string.IsNullOrEmpty(buttons[i].parent))
  49. {
  50. var btn = CreateButton(buttons[i], mainMenu.transform);
  51. buttons[i].button = btn;
  52. var subMenu = Instantiate(subMenuPrefab, subMenusParent);
  53. subMenu.name = $"SubMenu {buttons[i].title}";
  54. for (int a = 0; a < buttons.Count; a++)
  55. {
  56. if(buttons[a].parent == buttons[i].title)
  57. {
  58. var subBtn = CreateButton(buttons[a], subMenu.transform);
  59. buttons[a].button = subBtn;
  60. }
  61. }
  62. subMenu.SetActive(false);
  63. subMenus.Add(subMenu);
  64. }
  65. }
  66. }
  67. public override void DestroyMenu() => DestroyMenus();
  68. public void DestroyMenus()
  69. {
  70. for (int i = 0; i < subMenus.Count; i++)
  71. {
  72. Destroy(subMenus[i]);
  73. }
  74. subMenus = new List<GameObject>();
  75. #if UNITY_EDITOR
  76. DestroyImmediate(mainMenu);
  77. #else
  78. Destroy(mainMenu);
  79. #endif
  80. buttons = new List<MenuButtomData>();
  81. }
  82. public void OpenSubMenu(string title)
  83. {
  84. for (int i = 0; i < subMenus.Count; i++)
  85. {
  86. if($"SubMenu {title}" == subMenus[i].name) subMenus[i].SetActive(true);
  87. else subMenus[i].SetActive(false);
  88. }
  89. }
  90. public void EditorSetup()
  91. {
  92. useSubMenus = true;
  93. }
  94. }
  95. }