Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Facing issue with Drag and Drop in UI

Discussion in 'Getting Started' started by devmaheswari2017, Apr 28, 2024.

  1. devmaheswari2017

    devmaheswari2017

    Joined:
    Oct 9, 2022
    Posts:
    9
    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).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class DragableItem : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
    8. {
    9.     [SerializeField]
    10.     Image iconImage;
    11.     [SerializeField]
    12.     private Transform parentAfterDrag;
    13.  
    14.     private Canvas canvas;
    15.     private ItemDisplay itemDisplay;
    16.     private RectTransform rectTransform;
    17.     [SerializeField]
    18.     private InventorySlot inventorySlot;
    19.     private RectTransform startPosition;
    20.     private bool checkForItemSlotEmpty;
    21.     private Vector2 pos;
    22.  
    23.     //ToDo : The item should be dropabel in inventory slot
    24.  
    25.    // Setter
    26.     public void SetParentTransform(Transform transform)
    27.     {
    28.         parentAfterDrag = transform;
    29.     }
    30.  
    31.     public void SetInventorySlot(InventorySlot inventorySlot)
    32.     {
    33.         this.inventorySlot = inventorySlot;
    34.     }
    35.  
    36.     private void Awake()
    37.     {
    38.         canvas = GetComponentInParent<Canvas>();
    39.         rectTransform = GetComponent<RectTransform>();
    40.         itemDisplay = GetComponentInParent<ItemDisplay>();
    41.         startPosition = rectTransform;
    42.         pos = startPosition.anchoredPosition;
    43.     }
    44.     private void Update()
    45.     {
    46.         checkForItemSlotEmpty = itemDisplay.IsItemSOEmpty();
    47.     }
    48.  
    49.     public void OnBeginDrag(PointerEventData eventData)
    50.     {
    51.         //Debug.Log(" isItemSoEmpty : " + itemDisplay.IsItemSOEmpty() + " of item display " + itemDisplay);
    52.         if (checkForItemSlotEmpty == true)
    53.             return;
    54.  
    55.             iconImage.raycastTarget = false;
    56.             parentAfterDrag = transform.parent;
    57.             transform.SetParent(transform.root);
    58.             transform.SetAsLastSibling();
    59.     }
    60.  
    61.     public void OnDrag(PointerEventData eventData)
    62.     {
    63.         if (checkForItemSlotEmpty == true)
    64.             return;
    65.         itemDisplay.SetCanShowDiscWindow(false);
    66.         transform.position = Input.mousePosition;
    67.         //rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
    68.     }
    69.  
    70.     public void OnEndDrag(PointerEventData eventData)
    71.     {
    72.         if (checkForItemSlotEmpty == true)
    73.             return;
    74.  
    75.         //inventorySlot = eventData.pointerDrag.GetComponent<InventorySlot>();
    76.         transform.SetParent(parentAfterDrag);
    77.         itemDisplay.SetCanShowDiscWindow(true);
    78.         iconImage.raycastTarget = true;
    79.         Debug.Log("Parent after drag is : " + parentAfterDrag);
    80.         //if (inventorySlot == null)
    81.         //{
    82.         //    Debug.LogError("InventorySlot empty");
    83.         //    rectTransform.anchoredPosition = pos;
    84.         //}
    85.         ////// Check if the drop location is over a new item slot
    86.         ////if (eventData.pointerEnter != null && inventorySlot !=null)
    87.         ////{
    88.         ////    transform.SetParent(eventData.pointerEnter.transform);
    89.         ////}
    90.         //else
    91.         //{
    92.         //    transform.SetParent(inventorySlot.transform);
    93.         //}
    94.     }
    95.  
    96. }
    97.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. public class InventorySlot : MonoBehaviour,IPointerEnterHandler, IDropHandler
    6. {
    7.     [SerializeField]
    8.     DragableItem dragableItem;
    9.     public void OnDrop(PointerEventData eventData)
    10.     {
    11.         Debug.Log("OnDrop called");
    12.         GameObject dropped = eventData.pointerDrag;
    13.         if (dropped == null)
    14.         {
    15.             Debug.Log("No item was being dragged.");
    16.             return;
    17.         }
    18.  
    19.         Debug.Log("Dropped object: " + dropped.name);
    20.  
    21.         dragableItem = dropped.GetComponent<DragableItem>();
    22.         if (dragableItem == null)
    23.         {
    24.             Debug.Log("Dropped object is not a draggable item.");
    25.             return;
    26.         }
    27.  
    28.         dragableItem.SetParentTransform(transform);
    29.         Debug.Log("Dropping item into slot: " + dropped.name);
    30.     }
    31.     public void OnPointerEnter(PointerEventData eventData)
    32.     {
    33.  
    34.         //Debug.Log("Inside Inventory Slot");
    35.         //GameObject dropped = eventData.pointerDrag;
    36.         //dragableItem = dropped.GetComponent<DragableItem>();
    37.         //dragableItem.SetInventorySlot(this);
    38.     }
    39. }
    40.