Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: vallcrist on February 12, 2014, 02:56:06 AM

Title: OnDrop event not being called correctly
Post by: vallcrist on February 12, 2014, 02:56:06 AM
Hello guys. =)

I was trying to do some things with Drag & Drop today and stumbled in something weird.

Here's my setup :

Drag & drop itens : 50x50 px sprites, depth 15
Drag & drop container 1 : big sprite, depth 10
Drag & drop container 2 : 60x60px sprites, depth 10 ( An "inventory slot" )

The first time i drop an item in the container 2, it's ok, he receives the OnDrop event successfully, however, when i try and drop a second item on it, the object that receives the OnDrop is not the container, but the first item that was there, problably because of the depth and because they share the same collision area. This is bad because i'm doing my Swap logic on the OnDrop of the container, and if he doesn't receive the OnDrop event there's nothing I can do.

Also investigating NGUI code I saw that the OnDrop event isn't fired anywhere inside the UIDragAndDrop familiy of scripts, maybe the UIDragAndDropContainer should have a "OnItemDrop" callback/delegate?

Any ideas?
Title: Re: OnDrop event not being called correctly
Post by: ArenMook on February 13, 2014, 02:44:25 AM
OnDrop() is sent to whatever is underneath the mouse. If you never disabled the collider of the object you're dragging, then it will eat its own OnDrop() event.

Drag & drop example doesn't use OnDrop. it uses OnDragDropRelease (virtual function in UIDragDropitem), which is fired from OnDragEnd (which is an NGUI event sent to the dragged object when you stop dragging it).
Title: Re: OnDrop event not being called correctly
Post by: vallcrist on February 13, 2014, 11:23:44 PM
Ok, this way all logic must be made on the item being dropped.

Wouldn't it be nice if each UIDragDropContainer received an event of some sort when something gets added to it, though?
Title: Re: OnDrop event not being called correctly
Post by: ArenMook on February 14, 2014, 05:12:45 AM
It does. It gets the OnDrop -- but in the drag & drop example this event is not used.

As I said, you would have to make sure that the dragged object's collider is disabled while it's being dragged, or it will eat your events.
Title: Re: OnDrop event not being called correctly
Post by: vallcrist on February 16, 2014, 01:38:10 PM
I see..

But if I disable the collider of the dragged object after i set it in the single slot container, how would i move it out of it after?
Title: Re: OnDrop event not being called correctly
Post by: Yukichu on February 16, 2014, 02:19:49 PM
What I did was when you drop the object to the container, you cache either the object or the collider you just dropped it on.

Disable the collider.

When you move something again, enable the collider you cached.

It's a bit more than that, but it's the gist of it.
Title: Re: OnDrop event not being called correctly
Post by: ArenMook on February 17, 2014, 10:56:20 AM
Yup, just enable it again in OnDrop. It should only be disabled while dragging it.