Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

IDropHandler - onDrop() is not called on the Android Device but within the Unity Project.

Discussion in 'Android' started by spixel89, Aug 8, 2020.

  1. spixel89

    spixel89

    Joined:
    May 1, 2020
    Posts:
    2
    Hi all,

    i have implemented the IDropHandler interface with the onDrop() function.
    I'm making an inventory using Drag and Drop elements.

    My Project works fine within the Unity IDE.
    However, when I build my app on the Android device, the onDrop() function is never called. Everything else works just fine.
    I have no clue where to start to fix this.

    Does anybody have a suggestion?

    Best Regards
    Axel
     
    caglarenes likes this.
  2. spixel89

    spixel89

    Joined:
    May 1, 2020
    Posts:
    2
    I have tried several days to find a solution. However I have not started an entire new project to check for this problems root.

    I was not able to find a solution so far.
    However if somebody encounters this problem. Here is a workaround:

    Use the OnEndDrag function on your dragging object and Tag the object that shall take the Drop.

    Instead of the OnDrop function define your own function (OnDropRaycast) and pass your gameObject.

    Code (CSharp):
    1.     public void OnEndDrag(PointerEventData eventData)
    2.     {
    3.         if (eventData != null)
    4.         {
    5.             PointerEventData pointerData = new PointerEventData(EventSystem.current) { pointerId = -1, };
    6.             pointerData.position = eventData.position;
    7.             List<RaycastResult> results = new List<RaycastResult>();
    8.             EventSystem.current.RaycastAll(pointerData, results);
    9.             foreach (RaycastResult result in results)
    10.             {
    11.                 if (result.gameObject.tag.Equals("MyTag"))
    12.                 {
    13.                     result.gameObject.GetComponent<MergeSpotScript>().OnDropRaycast(this.gameObject);
    14.                 }
    15.             }
    16.         }
    17. ....
    18. }
    Best regards.
     
  3. tolga_akad

    tolga_akad

    Joined:
    Feb 27, 2020
    Posts:
    1
    Hello, thank you so much for posting your workaround, it worked perfectly! I blasted my brains on this issue for the past 4 hours and I think the issue is actually a bug in the StandaloneInputModule of the EventSystem.
    The problem doesn't only happen when you build but also when you use unity remote. So this got me thinking that there could be an issue with the touch controls of the input module.

    I'm new to coding but I compared the touch code with the click code;

    In the click code:
    Code (CSharp):
    1. private void ReleaseMouse(PointerEventData pointerEvent, GameObject currentOverGo)
    2.         {
    3.             ExecuteEvents.Execute(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerUpHandler);
    4.  
    5.             var pointerClickHandler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo);
    6.  
    7.             // PointerClick and Drop events
    8.             if (pointerEvent.pointerClick == pointerClickHandler && pointerEvent.eligibleForClick)
    9.             {
    10.                 ExecuteEvents.Execute(pointerEvent.pointerClick, pointerEvent, ExecuteEvents.pointerClickHandler);
    11.             }
    12.             if (pointerEvent.pointerDrag != null && pointerEvent.dragging)
    13.             {
    14.                 ExecuteEvents.ExecuteHierarchy(currentOverGo, pointerEvent, ExecuteEvents.dropHandler);
    15.             }
    where as in the touch code:
    Code (CSharp):
    1.  if (released)
    2.             {
    3.                 // Debug.Log("Executing pressup on: " + pointer.pointerPress);
    4.                 ExecuteEvents.Execute(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerUpHandler);
    5.  
    6.                 // Debug.Log("KeyCode: " + pointer.eventData.keyCode);
    7.  
    8.                 // see if we mouse up on the same element that we clicked on...
    9.                 var pointerClickHandler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo);
    10.  
    11.                 // PointerClick and Drop events
    12.                 if (pointerEvent.pointerClick == pointerClickHandler && pointerEvent.eligibleForClick)
    13.                 {
    14.                     ExecuteEvents.Execute(pointerEvent.pointerClick, pointerEvent, ExecuteEvents.pointerClickHandler);
    15.                 }
    16.                 else if (pointerEvent.pointerDrag != null && pointerEvent.dragging)
    17.                 {
    18.                     ExecuteEvents.ExecuteHierarchy(currentOverGo, pointerEvent, ExecuteEvents.dropHandler);
    19.                 }
    I think the problem is the "else if" at the end which should be "if". I maybe completely wrong but this is my best guess
     
  4. Creepy_BrainZ

    Creepy_BrainZ

    Joined:
    Jan 19, 2019
    Posts:
    3
    You Saved My Life, Thank You ;):)
     
  5. Xcopyoung

    Xcopyoung

    Joined:
    Apr 13, 2015
    Posts:
    1
    if u don`t want to add tag, maybe just get component and check if exist.

    Dropable droper = result.gameObject.GetComponent<Dropable>();

    if (droper != null)
    {
    droper.OnDropRaycast(eventData);
    }
     
  6. caglarenes

    caglarenes

    Joined:
    Jul 9, 2015
    Posts:
    45
    Thank you so much for your solution. Did you create a bug report about it?