Reputation: 775
I have 2 prefabs objects consisting in a panel with UI.Text inside. One contains the class for dragging and the other the dropping. However, even if the drag works fine the OnDrop() function is never executed. I have also set the blockRaycasts to false in the CanvasGroup that I added to the main Canvas.
GetComponentInParent<CanvasGroup>().blocksRaycasts = false;
Are there any reasons why the method OnDrop() implemented from the interface UnityEngine.EventSystems.IDropHandler may not be firing while I'm dragging an object into it?
public class ItemDropHandler : MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData eventData)
{
Debug.Log("Drop detected over the UI.Text"); //this is never shown
}
}
Upvotes: 3
Views: 6557
Reputation: 168
The problem is maybe caused by the fact, that you add the CanvasGroup to the MainCanvas and then set blocksRaycast to false for the complete MainCanvas itself. So basically all your inputs are going through your canvas without any effect.
The solution for the problem:
Here is some example code for the DragHandler:
public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("OnBeginDrag");
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
gameObject.transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
GetComponent<CanvasGroup>().blocksRaycasts = true;
Debug.Log("OnEndDrag");
}
}
Upvotes: 6