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

Differentiate Between Click/Drag on an Image

Discussion in 'UGUI & TextMesh Pro' started by mittense, Sep 4, 2014.

  1. mittense

    mittense

    Joined:
    Jul 25, 2013
    Posts:
    168
    I looked at the demo Drag and Drop example and it's actually something very close to what I was implementing in my project, but I'm running into a problem: OnBeginDrag seems to completely trump OnPointerClick.

    Basically, I like the drag-and-drop functionality, but if a player just clicks the image then something else happens and I can't figure out how to do this.
     
  2. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    Hi
    Just add IPointerClickHandler interface to demo DragMe script
    and move icon creation from OnBeginDrag to OnDrag event like so:

    Code (CSharp):
    1.  
    2. public void OnDrag(PointerEventData data)
    3.     {
    4.         if (m_DraggingIcon == null)
    5.         {
    6.             var canvas = FindInParents<Canvas>(gameObject);
    7.             if (canvas == null)
    8.                 return;
    9.  
    10.             // We have clicked something that can be dragged.
    11.             // What we want to do is create an icon for this.
    12.             m_DraggingIcon = new GameObject("icon");
    13.  
    14.             m_DraggingIcon.transform.SetParent(canvas.transform, false);
    15.             m_DraggingIcon.transform.SetAsLastSibling();
    16.  
    17.             var image = m_DraggingIcon.AddComponent<Image>();
    18.             // The icon will be under the cursor.
    19.             // We want it to be ignored by the event system.
    20.             m_DraggingIcon.AddComponent<IgnoreRaycast>();
    21.  
    22.             image.sprite = GetComponent<Image>().sprite;
    23.             image.SetNativeSize();
    24.  
    25.             if (dragOnSurfaces)
    26.                 m_DraggingPlane = transform as RectTransform;
    27.             else
    28.                 m_DraggingPlane = canvas.transform as RectTransform;
    29.         }
    30.          
    31.         SetDraggedPosition(data);
    32.     }
    33.  
    oh, and you can remove IBeginDragHandler because it's not suppose to do anything now
     
    mittense likes this.
  3. mittense

    mittense

    Joined:
    Jul 25, 2013
    Posts:
    168
    So, that's kind of what I was thinking, but the OnPointerClick event never gets triggered still.
     
  4. mittense

    mittense

    Joined:
    Jul 25, 2013
    Posts:
    168
    Well, I got it working; I just also had to add/handle IPointerDownHandler. Which is strange. But it works. So mini-win!
     
  5. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    That's weird it does works for me... Have you added IPointerClickHandler to DragMe class declaration?

    Code (CSharp):
    1.  
    2. public class DragMeClickMe : MonoBehaviour, ..., IPointerClickHandler
    3. {
    4.     ...
    5.     public void OnPointerClick(PointerEventData eventData)
    6.     {
    7.         Debug.Log("clicked me");
    8.     }
    9. }
    10.  
     
  6. mittense

    mittense

    Joined:
    Jul 25, 2013
    Posts:
    168
    Yeah, this is my current class definition that works (removing IPointerDownHandler will stop it from detecting clicks):

    public class PDUI_DragObject : MonoBehaviour, IPointerDownHandler, IPointerClickHandler, IDragHandler, IEndDragHandler