EncyclopediaUi.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.UI.InteractionHandler;
  7. [HideMonoScript]
  8. public class EncyclopediaUi : MonoBehaviour, IClickHandler
  9. {
  10. public EncyclopediaController controller;
  11. public GameObject menuGameObject;
  12. public GameObject subMenuGameObject;
  13. public GameObject viewsGameObject;
  14. public Transform menuContainer;
  15. public Transform subMenuContainer;
  16. public bool showDebug = false;
  17. public List<EncyclopediaUiView> views;
  18. private void Start()
  19. {
  20. if(menuGameObject == null) Debug.LogError("Missing Menu GameObject in EncyclopediaUi", this.gameObject);
  21. if(subMenuGameObject == null) Debug.LogError("Missing submenu GameObject in EncyclopediaUi", this.gameObject);
  22. if(viewsGameObject == null) Debug.LogError("Missing views GameObject in EncyclopediaUi", this.gameObject);
  23. }
  24. private void Update()
  25. {
  26. }
  27. [Button("Show Submenu"), ButtonGroup("EditorActions")]
  28. public void ShowSubmenu() => subMenuGameObject.SetActive(true);
  29. [Button("Hide Submenu"), ButtonGroup("EditorActions")]
  30. public void HideSubmenu() => subMenuGameObject.SetActive(false);
  31. public void Populate()
  32. {
  33. if(controller == null || menuContainer == null) return;
  34. Button[] oldButtons = menuContainer.GetComponentsInChildren<Button>();
  35. int counter = 0;
  36. if(showDebug) Debug.Log($"Destroying {oldButtons.Length} buttons");
  37. foreach (Button item in oldButtons) DestroyImmediate(item.gameObject);
  38. foreach (EncyclopediaCategory category in controller.categories)
  39. {
  40. var obj = Instantiate(category.buttonPrefab, new Vector3(), Quaternion.identity, menuContainer.transform);
  41. var btnData = obj.GetComponent<ButtonData>();
  42. if(btnData != null) btnData.Setup(category.title, category.icon);
  43. var handler = obj.GetComponent<ClickHandler>();
  44. if(handler != null) handler.Setup(category.layoutName, this);
  45. counter += 1;
  46. }
  47. if(showDebug) Debug.Log($"Created {counter} buttons");
  48. }
  49. public void OnClick(string title)
  50. {
  51. throw new System.NotImplementedException();
  52. }
  53. [System.Serializable]
  54. public class EncyclopediaUiView
  55. {
  56. [HorizontalGroup("Line", 0.4f), HideLabel] public string title;
  57. [HorizontalGroup("Line", 0.6f), HideLabel] public GameObject view;
  58. }
  59. }