ToolbarUi.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using KairoEngine.UI;
  6. using KairoEngine.UI.InteractionHandler;
  7. using KairoEngine.GameTools.InteractionTools;
  8. using Sirenix.OdinInspector;
  9. using KairoEngine.Core;
  10. using UniRx;
  11. namespace KairoEngine.GameTools.UI
  12. {
  13. public class ToolbarUi : MonoBehaviour, IClickHandler
  14. {
  15. public InteractionToolsManager interactionToolsManager;
  16. public MenuUI menuUi;
  17. private List<SelectedButton> selectButtons = new List<SelectedButton>();
  18. private int toolIndex;
  19. void Start()
  20. {
  21. CreateMenu();
  22. toolIndex = interactionToolsManager.currentTools;
  23. }
  24. void Update()
  25. {
  26. if(interactionToolsManager.currentTools != toolIndex)
  27. {
  28. toolIndex = interactionToolsManager.currentTools;
  29. for (int i = 0; i < interactionToolsManager.tools.Count; i++)
  30. {
  31. //if(selectButtons[i] == null) selectButtons[i] = menuUi.buttons[i].button.gameObject.GetComponent<SelectedButton>();
  32. if(toolIndex == i && selectButtons[i] != null) selectButtons[i].Select();
  33. else if(selectButtons[i] != null) selectButtons[i].Deselect();
  34. }
  35. }
  36. }
  37. public void OnClick(string title)
  38. {
  39. for (int i = 0; i < interactionToolsManager.tools.Count; i++)
  40. {
  41. if(interactionToolsManager.tools[i].name == title)
  42. {
  43. if(selectButtons[i] != null) selectButtons[i].Select();
  44. interactionToolsManager.ChangeTool(i);
  45. }
  46. else selectButtons[i].Deselect();
  47. }
  48. }
  49. [Button("Create")]
  50. public void CreateMenu()
  51. {
  52. if(interactionToolsManager == null) return;
  53. menuUi.DestroyMenu();
  54. selectButtons.Clear();
  55. List<MenuButtomData> menuData = new List<MenuButtomData>();
  56. for (int i = 0; i < interactionToolsManager.tools.Count; i++)
  57. {
  58. MenuButtomData data = new MenuButtomData();
  59. data.title = interactionToolsManager.tools[i].gameObject.name;
  60. data.showTooltip = false;
  61. data.subMenuParent = true;
  62. data.action = interactionToolsManager.tools[i].gameObject.name;
  63. data.graphic = interactionToolsManager.tools[i].GetComponent<IInteractionTool>().GetIcon();
  64. menuData.Add(data);
  65. }
  66. menuUi.buttons = menuData;
  67. menuUi.CreateMenu();
  68. Timer.ExecuteRealTime(100, () => {
  69. for (int i = 0; i < interactionToolsManager.tools.Count; i++)
  70. {
  71. selectButtons.Add(menuUi.buttons[i].button.gameObject.GetComponent<SelectedButton>());
  72. }
  73. selectButtons[interactionToolsManager.currentTools].Select();
  74. });
  75. }
  76. }
  77. }