From a2e4459b5c0a8092ab72d20047f99252ff55d67c Mon Sep 17 00:00:00 2001 From: Janis Date: Wed, 22 Feb 2023 13:50:33 +0100 Subject: [PATCH] Drag --- Assets/Scripts/Inventory.cs | 2 +- Assets/Scripts/UI/DraggableSlotContentUI.cs | 9 +++++---- Assets/Scripts/UI/InventoryUI.cs | 4 ---- Assets/Scripts/UI/SlotUI.cs | 1 - 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/Assets/Scripts/Inventory.cs b/Assets/Scripts/Inventory.cs index fc04a00..64a0c74 100644 --- a/Assets/Scripts/Inventory.cs +++ b/Assets/Scripts/Inventory.cs @@ -60,7 +60,7 @@ public class Inventory : MonoBehaviour { InventoryItem invItem = items[i]; Debug.Log(invItem); - if (invItem.item == null) + if (invItem?.item == null) { int toAdd = Mathf.Min(item.maxStackSize, remainingCount); diff --git a/Assets/Scripts/UI/DraggableSlotContentUI.cs b/Assets/Scripts/UI/DraggableSlotContentUI.cs index 9d35cdd..dc2cab8 100644 --- a/Assets/Scripts/UI/DraggableSlotContentUI.cs +++ b/Assets/Scripts/UI/DraggableSlotContentUI.cs @@ -1,16 +1,18 @@ using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; + + + public class DraggableSlotContentUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { - public int slotIndex; + public int slotIndex; // slotIndex is set by SlotUI [HideInInspector] public Transform parentAfterDrag; public Image image; public void OnBeginDrag(PointerEventData eventData) { - Debug.Log("Start Drag"); - parentAfterDrag = transform.parent; + slotIndex = parentAfterDrag.GetComponent().slotIndex; // Set slotIndex again to be safe, but should be already set transform.SetParent(transform.root); transform.SetAsLastSibling(); image.raycastTarget = false; @@ -25,7 +27,6 @@ public class DraggableSlotContentUI : MonoBehaviour, IBeginDragHandler, IDragHan public void OnEndDrag(PointerEventData eventData) { - Debug.Log("End drag"); transform.SetParent(parentAfterDrag); image.raycastTarget = true; // invoke diff --git a/Assets/Scripts/UI/InventoryUI.cs b/Assets/Scripts/UI/InventoryUI.cs index 32d2c5a..17bea10 100644 --- a/Assets/Scripts/UI/InventoryUI.cs +++ b/Assets/Scripts/UI/InventoryUI.cs @@ -32,14 +32,10 @@ public class InventoryUI : MonoBehaviour } private void UpdateSlots() { - Debug.Log("Update slots"); int i = 0; foreach (SlotUI slotUi in slotUIList) { - slotUi.SetInventoryItem(playerInventory.items[i]); - - i++; } } diff --git a/Assets/Scripts/UI/SlotUI.cs b/Assets/Scripts/UI/SlotUI.cs index e4c224e..91b514b 100644 --- a/Assets/Scripts/UI/SlotUI.cs +++ b/Assets/Scripts/UI/SlotUI.cs @@ -15,7 +15,6 @@ public class SlotUI : MonoBehaviour, IDropHandler private void Start() { UpdateSlot(); - contentObject.GetComponent().slotIndex = slotIndex; playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent(); } public void SetInventoryItem(InventoryItem newInventoryItem)