UiManager.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Sirenix.OdinInspector;
  6. namespace KairoEngine.UI
  7. {
  8. [HideMonoScript]
  9. public class UiManager : MonoBehaviour
  10. {
  11. public static UiManager instance;
  12. public bool showUiElements = true;
  13. [ListDrawerSettings(HideAddButton = true, HideRemoveButton = true, DraggableItems = false, Expanded = true, ShowPaging = false, ShowItemCount = true)]
  14. public List<ElementState> uiElements = new List<ElementState>();
  15. void Awake()
  16. {
  17. if(instance == null) instance = this;
  18. else Destroy(this);
  19. }
  20. void Update()
  21. {
  22. for (int i = 0; i < uiElements.Count; i++)
  23. {
  24. uiElements[i].gameState = uiElements[i].element.IsVisible();
  25. if(showUiElements == false)
  26. {
  27. if(uiElements[i].gameState == true) uiElements[i].element.Hide();
  28. }
  29. else
  30. {
  31. if(uiElements[i].desiredState == true && !uiElements[i].element.IsVisible()) uiElements[i].element.Show();
  32. else if(uiElements[i].desiredState == false && uiElements[i].element.IsVisible()) uiElements[i].element.Hide();
  33. }
  34. }
  35. }
  36. public static void RegisterElement(string title, IUiSystemElement element, Transform controllerTransform, bool currentState = false)
  37. {
  38. if(instance == null) return;
  39. var elementState = new ElementState(currentState, element.IsVisible(), title, element, controllerTransform);
  40. bool duplicate = false;
  41. for (int i = 0; i < instance.uiElements.Count; i++)
  42. {
  43. if(instance.uiElements[i].title == title)
  44. {
  45. duplicate = true;
  46. break;
  47. }
  48. }
  49. if(!duplicate) instance.uiElements.Add(elementState);
  50. if(instance.showUiElements && currentState) element.Show();
  51. else element.Hide();
  52. }
  53. public static void UnregisterElement(IUiSystemElement element)
  54. {
  55. if(instance == null) return;
  56. for (int i = 0; i < instance.uiElements.Count; i++)
  57. {
  58. if(instance.uiElements[i].element == element)
  59. {
  60. instance.uiElements[i].element.Hide();
  61. instance.uiElements.Remove(instance.uiElements[i]);
  62. return;
  63. }
  64. }
  65. }
  66. public static void ShowElement(IUiSystemElement element)
  67. {
  68. for (int i = 0; i < instance.uiElements.Count; i++)
  69. {
  70. if(instance.uiElements[i].element == element)
  71. {
  72. instance.uiElements[i].element.Show();
  73. instance.uiElements[i].desiredState = true;
  74. return;
  75. }
  76. }
  77. }
  78. public static void ShowElement(string title)
  79. {
  80. for (int i = 0; i < instance.uiElements.Count; i++)
  81. {
  82. if(instance.uiElements[i].title == title)
  83. {
  84. instance.uiElements[i].element.Show();
  85. instance.uiElements[i].desiredState = true;
  86. }
  87. }
  88. }
  89. public static void HideElement(IUiSystemElement element)
  90. {
  91. for (int i = 0; i < instance.uiElements.Count; i++)
  92. {
  93. if(instance.uiElements[i].element == element)
  94. {
  95. instance.uiElements[i].element.Hide();
  96. instance.uiElements[i].desiredState = false;
  97. return;
  98. }
  99. }
  100. }
  101. public static void HideElement(string title)
  102. {
  103. for (int i = 0; i < instance.uiElements.Count; i++)
  104. {
  105. if(instance.uiElements[i].title == title)
  106. {
  107. instance.uiElements[i].element.Hide();
  108. instance.uiElements[i].desiredState = false;
  109. }
  110. }
  111. }
  112. [System.Serializable]
  113. public class ElementState
  114. {
  115. [HideLabel, HorizontalGroup("h", 0.04f), Tooltip("Desired state")] public bool desiredState;
  116. [ReadOnly, HideLabel, HorizontalGroup("h", 0.04f), Tooltip("Current state")] public bool gameState;
  117. [ReadOnly, HideLabel, HorizontalGroup("h", 0.52f)] public string title = "";
  118. [HideInInspector] public IUiSystemElement element;
  119. [ReadOnly, HideLabel, HorizontalGroup("h", 0.4f)] public Transform controllerTransform;
  120. public ElementState(bool desiredState, bool gameState, string title, IUiSystemElement element, Transform controllerTransform)
  121. {
  122. this.desiredState = desiredState;
  123. this.gameState = gameState;
  124. this.title = title;
  125. this.element = element;
  126. this.controllerTransform = controllerTransform;
  127. }
  128. }
  129. }
  130. }