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.

51 lines
994 B

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