123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- 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<ElementState> uiElements = new List<ElementState>();
-
- 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;
- }
- }
- }
- }
|