So I am creating a Inventory system where I am trying to implement drag and drop functionality from my shop to my inventory slots, the dragging part is working fine but I am not able to drop my item on my inventory slots, i am using IDrophandler for the drop functionality and the weird part is that OnDrop function itself is not getting called properly for some reason, it’s getting called randomly (1 out of 5 times).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class DragableItem : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
[SerializeField]
Image iconImage;
[SerializeField]
private Transform parentAfterDrag;
private Canvas canvas;
private ItemDisplay itemDisplay;
private RectTransform rectTransform;
[SerializeField]
private InventorySlot inventorySlot;
private RectTransform startPosition;
private bool checkForItemSlotEmpty;
private Vector2 pos;
//ToDo : The item should be dropabel in inventory slot
// Setter
public void SetParentTransform(Transform transform)
{
parentAfterDrag = transform;
}
public void SetInventorySlot(InventorySlot inventorySlot)
{
this.inventorySlot = inventorySlot;
}
private void Awake()
{
canvas = GetComponentInParent<Canvas>();
rectTransform = GetComponent<RectTransform>();
itemDisplay = GetComponentInParent<ItemDisplay>();
startPosition = rectTransform;
pos = startPosition.anchoredPosition;
}
private void Update()
{
checkForItemSlotEmpty = itemDisplay.IsItemSOEmpty();
}
public void OnBeginDrag(PointerEventData eventData)
{
//Debug.Log(" isItemSoEmpty : " + itemDisplay.IsItemSOEmpty() + " of item display " + itemDisplay);
if (checkForItemSlotEmpty == true)
return;
iconImage.raycastTarget = false;
parentAfterDrag = transform.parent;
transform.SetParent(transform.root);
transform.SetAsLastSibling();
}
public void OnDrag(PointerEventData eventData)
{
if (checkForItemSlotEmpty == true)
return;
itemDisplay.SetCanShowDiscWindow(false);
transform.position = Input.mousePosition;
//rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData)
{
if (checkForItemSlotEmpty == true)
return;
//inventorySlot = eventData.pointerDrag.GetComponent<InventorySlot>();
transform.SetParent(parentAfterDrag);
itemDisplay.SetCanShowDiscWindow(true);
iconImage.raycastTarget = true;
Debug.Log("Parent after drag is : " + parentAfterDrag);
//if (inventorySlot == null)
//{
// Debug.LogError("InventorySlot empty");
// rectTransform.anchoredPosition = pos;
//}
////// Check if the drop location is over a new item slot
////if (eventData.pointerEnter != null && inventorySlot !=null)
////{
//// transform.SetParent(eventData.pointerEnter.transform);
////}
//else
//{
// transform.SetParent(inventorySlot.transform);
//}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class InventorySlot : MonoBehaviour,IPointerEnterHandler, IDropHandler
{
[SerializeField]
DragableItem dragableItem;
public void OnDrop(PointerEventData eventData)
{
Debug.Log("OnDrop called");
GameObject dropped = eventData.pointerDrag;
if (dropped == null)
{
Debug.Log("No item was being dragged.");
return;
}
Debug.Log("Dropped object: " + dropped.name);
dragableItem = dropped.GetComponent<DragableItem>();
if (dragableItem == null)
{
Debug.Log("Dropped object is not a draggable item.");
return;
}
dragableItem.SetParentTransform(transform);
Debug.Log("Dropping item into slot: " + dropped.name);
}
public void OnPointerEnter(PointerEventData eventData)
{
//Debug.Log("Inside Inventory Slot");
//GameObject dropped = eventData.pointerDrag;
//dragableItem = dropped.GetComponent<DragableItem>();
//dragableItem.SetInventorySlot(this);
}
}