12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using KairoEngine.Stockpiles;
- using KairoEngine.UI;
- namespace KairoEngine.Stockpiles.UI
- {
- public class StockpileGroupView : MonoBehaviour, IUiSystemElement
- {
- public string viewName = "StockpileGroupView";
- public StockpileManager manager;
- public string owner = "Player";
- public GameObject stockpileViewPrefab;
- public GameObject uiPanel;
- public Transform container;
- public Canvas canvas;
- public ContentSizeFitter contentSizeFitter;
- public bool startVisible = false;
- private bool isVisible = true;
- public void Hide()
- {
- if(canvas != null)
- {
- canvas.gameObject.SetActive(false);
- isVisible = false;
- }
- }
- public void Show()
- {
- if(canvas != null)
- {
- canvas.gameObject.SetActive(true);
- isVisible = true;
- }
- }
- public bool IsVisible() => isVisible;
- private void OnEnable() => UiManager.RegisterElement(viewName, this, this.transform, isVisible);
- private void OnDisable() => UiManager.UnregisterElement(this);
- private void Start()
- {
- if(startVisible) UiManager.ShowElement(this);
- else UiManager.HideElement(this);
- if(this.manager == null && StockpileManager.instance != null) manager = StockpileManager.instance;
- CreateStockpileViews();
- contentSizeFitter.enabled = false;
- }
- private void Update()
- {
- contentSizeFitter.enabled = true;
- }
- private void CreateStockpileViews()
- {
- if(stockpileViewPrefab == null)
- {
- Debug.LogError("Missing stockpileViewPrefab in StockpileGroupView component", this.gameObject);
- return;
- }
- for (int i = 0; i < manager.resourceTypes.Count; i++)
- {
- var obj = GameObject.Instantiate(stockpileViewPrefab, container);
- var stockpileView = obj.GetComponent<StockpileView>();
- if(stockpileView == null)
- {
- Debug.LogError($"The prefab \"{stockpileViewPrefab.name}\" doesn't have a StockpileView component! Aborting...", this.gameObject);
- return;
- }
- stockpileView.Setup(this, manager.resourceTypes[i]);
- }
- }
- }
- }
|