#How to see on which ui element did drag end?

1 messages · Page 1 of 1 (latest)

loud elbow
#

Hello guys i am trying to implement a simple drag and drop function, if image is dragged and released over a object with tag "CardHolder" it should be parented to that object, how can i achieve this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class CardInteraction : MonoBehaviour, IPointerClickHandler, IDragHandler, IBeginDragHandler, IEndDragHandler
{
    [SerializeField] CardDisplay cardDisplay;

    static Transform myParent;
    static Transform canvas = null;

    bool isDraging = false;
    private void Awake()
    {
        if (canvas == null)
        {
            canvas = GetComponentInParent<Canvas>().gameObject.transform;
            myParent = GameObject.FindGameObjectWithTag("CardsInHand").transform;
        }
    }


    public void OnBeginDrag(PointerEventData eventData)
    {
        isDraging = true;
        transform.SetParent(canvas);
    }

    public void OnDrag(PointerEventData eventData)
    {
        transform.position = eventData.position;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        //check for object with CardHolder tag, if not found set card back to previus parrent
        transform.SetParent(myParent);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (isDraging)
        {
            isDraging = false;
            return;
        }
        cardDisplay.OnClick();
    }
}

ripe osprey
# loud elbow Hello guys i am trying to implement a simple drag and drop function, if image is...

not the direct answer, there might be a better way, but you can just raycast onto the canvas at the position the PointerEventData returns.
this video should give you all the necessary info
https://www.youtube.com/watch?v=ptmum1FXiLE

✅ Get the Project files and Utilities at https://unitycodemonkey.com/video.php?v=ptmum1FXiLE
This is a Quick Tip for testing if the mouse is over the UI.

If you have any questions post them in the comments and I'll do my best to answer them.

🔔 Subscribe for more Unity Tutorials https://www.youtube.com/channel/UCFK6NCbuCIVzA6Yj1G_ZqCg?sub_confi...

▶ Play video