Author Topic: How to stop a drag?  (Read 3595 times)

Neverdyne

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
How to stop a drag?
« on: January 16, 2014, 08:15:06 PM »
Let's say the player is dragging a DragDropItem object, how can you stop the drag and make the object return to its previous parent? Basically as if the player had released the item on a non-DragDropContainer surface.

UPDATE: So I managed to make the object return to it's parent, but the Camera keeps sending drag messages. So basically what I need is to make the Camera stop sending drag messages even if the player stays with the mouse button pressed down.
« Last Edit: January 16, 2014, 08:44:07 PM by Neverdyne »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to stop a drag?
« Reply #1 on: January 16, 2014, 10:16:14 PM »
You need to clear UICamera.currentTouch.drag, and possibly UICamera.currentTouch.press.

Neverdyne

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: How to stop a drag?
« Reply #2 on: January 17, 2014, 11:34:55 AM »
Thank you for the reply, I'm trying to set:

UICamera.currentTouch.dragged = null;
UICamera.currentTouch.pressed = null;

However, I get an "Object reference not set to an instance of an object." error in Unity. Could this be because the "currentTouch" member is static?


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to stop a drag?
« Reply #3 on: January 17, 2014, 03:02:44 PM »
'currentTouch' is only valid during an NGUI event. You won't be able to set it from just anywhere -- it can only be set from an NGUI event such as OnPress, OnClick, OnDrag, etc.

Neverdyne

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: How to stop a drag?
« Reply #4 on: January 17, 2014, 04:11:58 PM »
Thank you for replying again! I've managed to make it work based on what you said. For future reference to anyone interested in this, what I did was alter the DragDropItem script slightly, giving it a new public member:

  1. public bool stopDrag = false;

And then changing the OnDrag method:

  1. void OnDrag(Vector2 delta)
  2. {
  3.     if (!enabled || mTouchID != UICamera.currentTouchID) return;
  4.     mTrans.localPosition += (Vector3)delta * mRoot.pixelSizeAdjustment;
  5.  
  6.     if (stopDrag)
  7.     {
  8.         UICamera.currentTouch.dragged = null;
  9.         UICamera.currentTouch.pressed = null;
  10.         stopDrag = false;
  11.         OnDragEnd();
  12.     }
  13. }

Then the only thing you need to do from an external source to stop the drag is set the "stopDrag" bool as true. So far it works.