You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
964 B

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. public class InventoryUI : MonoBehaviour
  4. {
  5. public Inventory inventory;
  6. [SerializeField]
  7. private BagItem ItemPrefab;
  8. [SerializeField]
  9. private Transform Parent;
  10. public List<BagItem> content { get; private set; }
  11. private void Start()
  12. {
  13. UpdateUI();
  14. }
  15. private void OnEnable()
  16. {
  17. inventory.OnItemsUpdated += UpdateUI;
  18. }
  19. private void OnDisable()
  20. {
  21. inventory.OnItemsUpdated -= UpdateUI;
  22. }
  23. [ContextMenu("Update UI")]
  24. public void UpdateUI()
  25. {
  26. foreach(Transform child in Parent)
  27. Destroy(child.gameObject);
  28. content = new List<BagItem>();
  29. foreach (Inventory.Data data in inventory.BagItems)
  30. {
  31. BagItem item = Instantiate(ItemPrefab.gameObject, Parent).GetComponent<BagItem>();
  32. item.Initialise(data);
  33. content.Add(item);
  34. }
  35. }
  36. }