86 lines
2.5 KiB
C#
86 lines
2.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
internal class SlotInfo
|
|
{
|
|
public VisualElement slotElement { get; private set; }
|
|
public Label amountLabel { get; private set; }
|
|
|
|
public SlotInfo(VisualElement slotElement)
|
|
{
|
|
this.slotElement = slotElement;
|
|
amountLabel = slotElement.Q<Label>(className: "amount");
|
|
}
|
|
}
|
|
|
|
[RequireComponent(typeof(UIDocument))]
|
|
[RequireComponent(typeof(ResourceStorage))]
|
|
public class ResourceStorageView : MonoBehaviour
|
|
{
|
|
private UIDocument uiDocument;
|
|
private ResourceStorage storage;
|
|
|
|
private SlotInfo metalSlot;
|
|
private SlotInfo electronicsSlot;
|
|
private SlotInfo energySlot;
|
|
private SlotInfo moneySlot;
|
|
|
|
void OnEnable()
|
|
{
|
|
storage = GetComponent<ResourceStorage>();
|
|
storage.Inventory.RegisterOnChangeCallback(OnStorageChange);
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
storage.Inventory.UnregisterOnChangeCallback(OnStorageChange);
|
|
}
|
|
|
|
private void OnStorageChange()
|
|
{
|
|
UpdateVisuals();
|
|
}
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
uiDocument = GetComponent<UIDocument>();
|
|
var rootElement = uiDocument.rootVisualElement;
|
|
|
|
metalSlot = new SlotInfo(rootElement.Q<VisualElement>("MetalSlot"));
|
|
electronicsSlot = new SlotInfo(rootElement.Q<VisualElement>("ElectronicsSlot"));
|
|
energySlot = new SlotInfo(rootElement.Q<VisualElement>("EnergySlot"));
|
|
moneySlot = new SlotInfo(rootElement.Q<VisualElement>("MoneySlot"));
|
|
|
|
UpdateVisuals();
|
|
}
|
|
|
|
private void UpdateVisuals()
|
|
{
|
|
UpdateSlotVisuals(
|
|
metalSlot,
|
|
storage.Inventory.GetAmount(ResourceRegistry.Instance.Metal.Name)
|
|
);
|
|
UpdateSlotVisuals(
|
|
electronicsSlot,
|
|
storage.Inventory.GetAmount(ResourceRegistry.Instance.Electronics.Name)
|
|
);
|
|
UpdateSlotVisuals(
|
|
energySlot,
|
|
storage.Inventory.GetAmount(ResourceRegistry.Instance.Energy.Name)
|
|
);
|
|
UpdateSlotVisuals(
|
|
moneySlot,
|
|
storage.Inventory.GetAmount(ResourceRegistry.Instance.Money.Name)
|
|
);
|
|
}
|
|
|
|
private void UpdateSlotVisuals(SlotInfo slot, float amount)
|
|
{
|
|
slot.slotElement.style.display = DisplayStyle.Flex;
|
|
// slot.slotElement.style.display = resource.Unlocked ? DisplayStyle.Flex : DisplayStyle.None;
|
|
slot.amountLabel.text = amount.ToString();
|
|
}
|
|
}
|