Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

difference between OnEndDrag and OnPointerUp

Discussion in 'UGUI & TextMesh Pro' started by ALI3D69, Mar 1, 2015.

  1. ALI3D69

    ALI3D69

    Joined:
    Apr 20, 2014
    Posts:
    19
    hi
    what is the difference between OnEndDrag and OnPointerUp? i want to call a function when stop dragging while the pointer is still down (Pointer.delta = vector2.zero). shouldn't OnEndDrag do this? what should i use instead?

    how can i call function every tick between pointerDown and pointerUp? i want to use pointer.IsPointerMoving() to fix this problem.
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
  3. ALI3D69

    ALI3D69

    Joined:
    Apr 20, 2014
    Posts:
    19
    thank you for your answer
    i think OnPointerUp called even when pointer didnt move.

    so how can i call function every tick between pointerDown and pointerUp? i want somthing like OnDontDragAnyMore. i want to call a function once when stop dragging while the pointer is still down.

    i can check it in update; but in update, how can i know that pointer is still down or is it moving or not?
     
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Correct, PointerUp has nothing to do with mouse movement, just touch/clicks

    If you want a script to respect PointerUp and PointerDown only, then write a new script implementing the IPointerDownhandler and IPointerUpHandler interfaces and do what ever login you want inside. then attach it to those components you want it to.

    if you want to capture ALL pointer up / down events, then you will need an inputmodule type script (like the standalone input module) attached to the event system to do something with them
     
  5. ALI3D69

    ALI3D69

    Joined:
    Apr 20, 2014
    Posts:
    19
    thanks again but i don't understand what you said!
    and this is the best i can do and i think its not optimized because of existence of FixedUpdate. i appreciate if someone show me a better way.

    why this EventSystems is so incomplete? we cant recognize 2 pointer. and this problem.

    Code (CSharp):
    1. public class RacketTouchMoveL : MonoBehaviour , IPointerDownHandler , IDragHandler , IPointerUpHandler
    2. {
    3.     PointerEventData myPointerEventData = null;
    4.     bool myMoving = false;
    5.  
    6.  
    7.     void FixedUpdate()
    8.     {  
    9.         if (myPointerEventData != null)
    10.         {
    11.             if ( !myPointerEventData.IsPointerMoving() && myMoving == true )
    12.             {
    13.                 // it call every time that pointer stop moving while, pointer is down
    14.                 MyStopRacket();
    15.                 myMoving = false;
    16.             }
    17.         }
    18.     }
    19.  
    20.     public void OnPointerDown ( PointerEventData myPointer )
    21.     {      
    22.         myPointerEventData = myPointer;
    23.     }
    24.  
    25.     public void OnPointerUp ( PointerEventData myPointer )
    26.     {
    27.         myMoving = true;
    28.         myPointerEventData = null;
    29.         MyStopMoving();
    30.     }
    31.        
    32.     public void OnDrag ( PointerEventData myPointer )
    33.     {
    34.         MyStartMoving( myPointer );
    35.     }
    36.  
    37.     ...
    38.  
    39. }
     
  6. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    I'm not sure whether you can keep references to pointer event data like that and expect it to continuously update. I could be wrong though.

    In case it helps, here's some code from a 2D game that lets the player peek around the map a bit based on mouse cursor movement. I didn't like it in the end, but it worked as expected. Its needs were different to yours, but one thing it had in common was that it needed to perform processing every frame even when drag events did not arrive - in your case you want to do something while the button is held down even if the mouse doesn't move, while in my case I wanted to perform drag-style processing even when no buttons were held down.

    Generally I assumed that I would have to fall back to the old Input methods for tracking the cursor, but I didn't check that.

    Code (csharp):
    1.  
    2.         public void OnPointerEnter(PointerEventData eventData)
    3.         {
    4.             _pointerOver = true;
    5.             _pointerPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    6.         }
    7.  
    8.         public void OnPointerExit(PointerEventData eventData)
    9.         {
    10.             _pointerOver = false;
    11.         }
    12.  
    13.         public void Update()
    14.         {
    15.             if (_pointerOver)
    16.             {
    17.                 var mousePos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
    18.                 var delta = _pointerPosition - mousePos;
    19.                 Camera.main.transform.position = Camera.main.transform.position -
    20.                                  2 * (Vector3)delta * 0.2f;
    21.                 _pointerPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    22.             }
    23.         }
    24.  
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    OnDrag is called every frame while you are dragging.

    If you want to call something only when the player first starts dragging an object, you can use OnBeginDrag (implementing the IBeginDragHandler interface). Note, however, that OnBeginDrag doesn't seem to get called unless you also have an OnDrag.

    I'm not completely sure what you mean by "stop dragging while the pointer is still down". In standard vocabulary, the "drag" isn't over until you let go, even if the mouse isn't moving.

    If you're trying to detect when the mouse stops moving, you'll need to remember its position from the previous frame and compare that to its position in the new frame to see how much they've changed. But "not moving" is actually a bit of a tricky concept, because the mouse may make tiny movements even when the user is trying to hold it still, and conversely, the pointer may pause momentarily even while the player is in the process of moving it. So you would probably need to check for something like "hasn't moved more than 5 canvas units over the last quarter of a second".
     
  8. Yomi050505

    Yomi050505

    Joined:
    Jul 13, 2021
    Posts:
    1
    I might be 7 years late but he has a point in unity 2020 when you stope dragging OnDrag isn't being called/run anymore.