Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Neverdyne on January 16, 2014, 08:15:06 PM

Title: How to stop a drag?
Post by: Neverdyne 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.
Title: Re: How to stop a drag?
Post by: ArenMook on January 16, 2014, 10:16:14 PM
You need to clear UICamera.currentTouch.drag, and possibly UICamera.currentTouch.press.
Title: Re: How to stop a drag?
Post by: Neverdyne 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?

Title: Re: How to stop a drag?
Post by: ArenMook 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.
Title: Re: How to stop a drag?
Post by: Neverdyne 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.