0

I am currently trying to make a drag and drop system where i have a dragable UI object which i can place on a other object. But i don't know how i can get the UI object below the mouse and the dragable UI object.

This is the code of my dragableObject:

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

public class DragHandeler : MonoBehaviour, IBeginDragHandler, IDragHandler, 
IEndDragHandler
{
  public Transform parentToReturnTo = null;
  public Transform placeHolderParent = null;


  public void OnBeginDrag(PointerEventData eventData)
  {
      parentToReturnTo = transform.parent;
  }

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

  }

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

and this code for my place holder:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 public class PlaceHolder : MonoBehaviour, IDropHandler, 
 IPointerEnterHandler, IPointerExitHandler
 {
    public void OnPointerEnter(PointerEventData eventData)
    {
         Transform test = eventData.pointerDrag.GetComponent<Transform>();
        Debug.Log(test.name);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
    }

    public void OnDrop(PointerEventData eventData)
   {
   }
}

the problem is the OnPointerEnter Function isn't called always when it should be, it only calls it sometimes when i am over the object with my mouse. Is there maybe a other way on how i can get the current object below the dragable object?

1

1 Answer 1

1

So, the issue is that your 'draggable' is blocking the raycast for any objects behind that object, and as a result - your PlaceHolder doesn't triggering any events because a ray is always blocked by your 'draggable'. The easiest solution is just disable raycastTarget property at the beginning of the drag and re-enable it at the end of drag.

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

public class DragHandeler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
  public Transform parentToReturnTo = null;
  public Transform placeHolderParent = null;

  public void OnBeginDrag(PointerEventData eventData)
  {
      // disable 'raycast target' for your draggable object
      GetComponent<Image>().raycastTarget = false;

      parentToReturnTo = transform.parent;
  }

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

  public void OnEndDrag(PointerEventData eventData)
  {
      // enable it back when drag is over
      GetComponent<Image>().raycastTarget = true;

      transform.position = parentToReturnTo.position;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.