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
888 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. [SerializeField]
  7. private Inventory inventory;
  8. [SerializeField]
  9. private BagItem ItemPrefab;
  10. [SerializeField]
  11. private Transform Parent;
  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. foreach (Inventory.Data data in inventory.BagItems)
  30. {
  31. BagItem item = Instantiate(ItemPrefab.gameObject, Parent).GetComponent<BagItem>();
  32. item.Initialise(data);
  33. }
  34. }
  35. }