UiManager.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if(instance == null) return;
  69. for (int i = 0; i < instance.uiElements.Count; i++)
  70. {
  71. if(instance.uiElements[i].element == element)
  72. {
  73. instance.uiElements[i].element.Show();
  74. instance.uiElements[i].desiredState = true;
  75. return;
  76. }
  77. }
  78. }
  79. public static void ShowElement(string title)
  80. {
  81. if(instance == null) return;
  82. for (int i = 0; i < instance.uiElements.Count; i++)
  83. {
  84. if(instance.uiElements[i].title == title)
  85. {
  86. instance.uiElements[i].element.Show();
  87. instance.uiElements[i].desiredState = true;
  88. }
  89. }
  90. }
  91. public static void HideElement(IUiSystemElement element)
  92. {
  93. if(instance == null) return;
  94. for (int i = 0; i < instance.uiElements.Count; i++)
  95. {
  96. if(instance.uiElements[i].element == element)
  97. {
  98. instance.uiElements[i].element.Hide();
  99. instance.uiElements[i].desiredState = false;
  100. return;
  101. }
  102. }
  103. }
  104. public static void HideElement(string title)
  105. {
  106. for (int i = 0; i < instance.uiElements.Count; i++)
  107. {
  108. if(instance.uiElements[i].title == title)
  109. {
  110. instance.uiElements[i].element.Hide();
  111. instance.uiElements[i].desiredState = false;
  112. }
  113. }
  114. }
  115. [System.Serializable]
  116. public class ElementState
  117. {
  118. [HideLabel, HorizontalGroup("h", 0.04f), Tooltip("Desired state")] public bool desiredState;
  119. [ReadOnly, HideLabel, HorizontalGroup("h", 0.04f), Tooltip("Current state")] public bool gameState;
  120. [ReadOnly, HideLabel, HorizontalGroup("h", 0.52f)] public string title = "";
  121. [HideInInspector] public IUiSystemElement element;
  122. [ReadOnly, HideLabel, HorizontalGroup("h", 0.4f)] public Transform controllerTransform;
  123. public ElementState(bool desiredState, bool gameState, string title, IUiSystemElement element, Transform controllerTransform)
  124. {
  125. this.desiredState = desiredState;
  126. this.gameState = gameState;
  127. this.title = title;
  128. this.element = element;
  129. this.controllerTransform = controllerTransform;
  130. }
  131. }
  132. }
  133. }