InventoryUIController.cs 1013 B

123456789101112131415161718192021222324252627282930313233343536
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using TMPro;
  4. public class InventoryUIController : MonoBehaviour
  5. {
  6. public GameObject slotPrefab;
  7. public Transform gridContainer;
  8. private CharacterInGroup boundCharacter;
  9. public void Bind(CharacterInGroup character)
  10. {
  11. boundCharacter = character;
  12. Refresh();
  13. }
  14. public void Refresh()
  15. {
  16. foreach (Transform child in gridContainer)
  17. Destroy(child.gameObject);
  18. foreach (var item in boundCharacter.inventory.items)
  19. {
  20. GameObject slot = Instantiate(slotPrefab, gridContainer);
  21. slot.GetComponentInChildren<Image>().sprite = item.icon;
  22. slot.GetComponent<DragDropItem>().Init(item, this, boundCharacter);
  23. }
  24. }
  25. public void TransferItem(InventoryItem item, CharacterInGroup receiver)
  26. {
  27. boundCharacter.inventory.items.Remove(item);
  28. receiver.inventory.items.Add(item);
  29. Refresh();
  30. }
  31. }