Tasharen Entertainment Forum
Support => NGUI 3 Support => Topic started 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?
-
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).
-
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?
-
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.
-
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?
-
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.
-
Yup, just enable it again in OnDrop. It should only be disabled while dragging it.