UiSystemLibrary.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. namespace KairoEngine.UI
  6. {
  7. [CreateAssetMenu(fileName = "Ui Library", menuName = "KairoEngine/UiSystemLibrary"), HideMonoScript]
  8. public class UiSystemLibrary : ScriptableObject
  9. {
  10. public List<UiSystemLibraryItem> elements = new List<UiSystemLibraryItem>();
  11. public void Load(Transform parent)
  12. {
  13. for (int i = 0; i < elements.Count; i++) InstantiateElement(parent, i);
  14. }
  15. private void InstantiateElement(Transform parent, int index)
  16. {
  17. if(elements[index] == null)
  18. {
  19. Debug.LogError("Error loading UI Library item, missing prefab.");
  20. return;
  21. }
  22. var obj = GameObject.Instantiate(elements[index].uiPrefab, parent);
  23. obj.name = (elements[index].uiPrefab.name).Replace("Prefab", "");
  24. IUiSystemElement el = obj.GetComponent<IUiSystemElement>();
  25. if(el == null) el = obj.GetComponentInChildren<IUiSystemElement>();
  26. if(el != null)
  27. {
  28. if(elements[index].visibleOnStart) UiManager.ShowElement(el);
  29. else UiManager.HideElement(el);
  30. }
  31. else
  32. {
  33. if(elements[index].visibleOnStart) obj.SetActive(true);
  34. else obj.SetActive(false);
  35. }
  36. }
  37. }
  38. [System.Serializable]
  39. public class UiSystemLibraryItem
  40. {
  41. [HorizontalGroup("line", 0.015f), HideLabel] public bool visibleOnStart = false;
  42. [HorizontalGroup("line", 0.985f), HideLabel] public GameObject uiPrefab;
  43. }
  44. }