using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Sirenix.OdinInspector; namespace KairoEngine.UI { [HideMonoScript] public class UiManager : MonoBehaviour { public static UiManager instance; public bool showUiElements = true; [ListDrawerSettings(HideAddButton = true, HideRemoveButton = true, DraggableItems = false, Expanded = true, ShowPaging = false, ShowItemCount = true)] public List uiElements = new List(); void Awake() { if(instance == null) instance = this; else Destroy(this); } void Update() { for (int i = 0; i < uiElements.Count; i++) { uiElements[i].gameState = uiElements[i].element.IsVisible(); if(showUiElements == false) { if(uiElements[i].gameState == true) uiElements[i].element.Hide(); } else { if(uiElements[i].desiredState == true && !uiElements[i].element.IsVisible()) uiElements[i].element.Show(); else if(uiElements[i].desiredState == false && uiElements[i].element.IsVisible()) uiElements[i].element.Hide(); } } } public static void RegisterElement(string title, IUiSystemElement element, Transform controllerTransform, bool currentState = false) { if(instance == null) return; var elementState = new ElementState(currentState, element.IsVisible(), title, element, controllerTransform); bool duplicate = false; for (int i = 0; i < instance.uiElements.Count; i++) { if(instance.uiElements[i].title == title) { duplicate = true; break; } } if(!duplicate) instance.uiElements.Add(elementState); if(instance.showUiElements && currentState) element.Show(); else element.Hide(); } public static void UnregisterElement(IUiSystemElement element) { if(instance == null) return; for (int i = 0; i < instance.uiElements.Count; i++) { if(instance.uiElements[i].element == element) { instance.uiElements[i].element.Hide(); instance.uiElements.Remove(instance.uiElements[i]); return; } } } public static void ShowElement(IUiSystemElement element) { if(instance == null) return; for (int i = 0; i < instance.uiElements.Count; i++) { if(instance.uiElements[i].element == element) { instance.uiElements[i].element.Show(); instance.uiElements[i].desiredState = true; return; } } } public static void ShowElement(string title) { if(instance == null) return; for (int i = 0; i < instance.uiElements.Count; i++) { if(instance.uiElements[i].title == title) { instance.uiElements[i].element.Show(); instance.uiElements[i].desiredState = true; } } } public static void HideElement(IUiSystemElement element) { if(instance == null) return; for (int i = 0; i < instance.uiElements.Count; i++) { if(instance.uiElements[i].element == element) { instance.uiElements[i].element.Hide(); instance.uiElements[i].desiredState = false; return; } } } public static void HideElement(string title) { for (int i = 0; i < instance.uiElements.Count; i++) { if(instance.uiElements[i].title == title) { instance.uiElements[i].element.Hide(); instance.uiElements[i].desiredState = false; } } } [System.Serializable] public class ElementState { [HideLabel, HorizontalGroup("h", 0.04f), Tooltip("Desired state")] public bool desiredState; [ReadOnly, HideLabel, HorizontalGroup("h", 0.04f), Tooltip("Current state")] public bool gameState; [ReadOnly, HideLabel, HorizontalGroup("h", 0.52f)] public string title = ""; [HideInInspector] public IUiSystemElement element; [ReadOnly, HideLabel, HorizontalGroup("h", 0.4f)] public Transform controllerTransform; public ElementState(bool desiredState, bool gameState, string title, IUiSystemElement element, Transform controllerTransform) { this.desiredState = desiredState; this.gameState = gameState; this.title = title; this.element = element; this.controllerTransform = controllerTransform; } } } }