2D Toolkit Forum

2D Toolkit => Support => Topic started by: LaserDinosaur on September 11, 2015, 01:16:34 am

Title: Enhanced Drag and Drop for 2d toolkit - looking for code feedback.
Post by: LaserDinosaur on September 11, 2015, 01:16:34 am
Hi all

As part of the game I'm working on I needed a drag and drop system that worked closer to how Unity's canvas+event system works. 2d toolkit has a drag and drop system, but it's very simple and only maps a button to your mouse position.

I made up a small enhancement that lets you:

(https://dl.dropboxusercontent.com/u/20830426/tk2ddragdrop/DragDrop2.gif) (https://dl.dropboxusercontent.com/u/20830426/tk2ddragdrop/dragDrop_sampleCode.png)

Now with that, I was wondering if you guys could take a peek and let me know if you spot anything that could be improved. I'll eventually put a final version in the announcements forum with documentation, but I wanted to make sure it was following best practices first. For example, one possible point of contention is that on line 259 of tk2dUIDragItem_Custom.cs I'm using a .Where statement to filter out raycasts that are hitting themselves. Unikron seems to avoid using statements like this, probably to ensure iPhone support, but my alternatives always wound up being big cludgy messes of code. If anyone has any ways to replace that, do let me know.

Download via dropbox (https://dl.dropboxusercontent.com/u/20830426/tk2ddragdrop/DragAndDrop.unitypackage)

tk2dUIDragItem_Custom.cs contains all the code to get the drag and drop to work (well, including tk2d of course). It's an extended version of the tk2dUIDragItem script so bits and pieces might look familiar. There are also two example files called "DragWithIcon" and "DropZOne".

If you want to get an idea of how it works, the code section of this video (https://youtu.be/c47QYgsJrWc?t=6m4s) is basically how this extension works.
Title: Re: Enhanced Drag and Drop for 2d toolkit - looking for code feedback.
Post by: unikronsoftware on September 15, 2015, 03:06:46 pm
Hi,

Not sure why but when i loaded the scene, the drag behaviour didn't work properly. It did work once I imported it in again a second time...

Regarding coding style it seems fine - we didn't use any Linq extensions because we had memory allocation issues back in the day. I don't know if its still a problem in Unity 5 now. Looks really good, awesome work!
Title: Re: Enhanced Drag and Drop for 2d toolkit - looking for code feedback.
Post by: LaserDinosaur on September 16, 2015, 07:37:53 pm
Hi Unikron

Thanks for the feedback, feels quite nice to get the thumbs up from the mastermind himself  ;D

As for the package not loading first time, I wonder if it was because I exported the package from Unity 5.1. Unity packages contain thier own meta files, so perhaps the first time you opened it the package got updated to 5.2, and then only on the second attempt it loaded properly. I'll re-export a 5.2 version, hopefully that fixes it.

When I get time I'll do a final version with some samples and put it up on the 'Add-ons and Extensions' forum.

Cheers!