StockpileGroupView.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using KairoEngine.Stockpiles;
  6. using KairoEngine.UI;
  7. namespace KairoEngine.Stockpiles.UI
  8. {
  9. public class StockpileGroupView : MonoBehaviour, IUiSystemElement
  10. {
  11. public string viewName = "StockpileGroupView";
  12. public StockpileManager manager;
  13. public string owner = "Player";
  14. public GameObject stockpileViewPrefab;
  15. public GameObject uiPanel;
  16. public Transform container;
  17. public Canvas canvas;
  18. public ContentSizeFitter contentSizeFitter;
  19. public bool startVisible = false;
  20. private bool isVisible = true;
  21. public void Hide()
  22. {
  23. if(canvas != null)
  24. {
  25. canvas.gameObject.SetActive(false);
  26. isVisible = false;
  27. }
  28. }
  29. public void Show()
  30. {
  31. if(canvas != null)
  32. {
  33. canvas.gameObject.SetActive(true);
  34. isVisible = true;
  35. }
  36. }
  37. public bool IsVisible() => isVisible;
  38. private void OnEnable() => UiManager.RegisterElement(viewName, this, this.transform, isVisible);
  39. private void OnDisable() => UiManager.UnregisterElement(this);
  40. private void Start()
  41. {
  42. if(startVisible) UiManager.ShowElement(this);
  43. else UiManager.HideElement(this);
  44. if(this.manager == null && StockpileManager.instance != null) manager = StockpileManager.instance;
  45. CreateStockpileViews();
  46. contentSizeFitter.enabled = false;
  47. }
  48. private void Update()
  49. {
  50. contentSizeFitter.enabled = true;
  51. }
  52. private void CreateStockpileViews()
  53. {
  54. if(stockpileViewPrefab == null)
  55. {
  56. Debug.LogError("Missing stockpileViewPrefab in StockpileGroupView component", this.gameObject);
  57. return;
  58. }
  59. for (int i = 0; i < manager.resourceTypes.Count; i++)
  60. {
  61. var obj = GameObject.Instantiate(stockpileViewPrefab, container);
  62. var stockpileView = obj.GetComponent<StockpileView>();
  63. if(stockpileView == null)
  64. {
  65. Debug.LogError($"The prefab \"{stockpileViewPrefab.name}\" doesn't have a StockpileView component! Aborting...", this.gameObject);
  66. return;
  67. }
  68. stockpileView.Setup(this, manager.resourceTypes[i]);
  69. }
  70. }
  71. }
  72. }