Search Unity

A generic Drag & Drop system utilizing Unity's event system

Discussion in 'UGUI & TextMesh Pro' started by coeing, Dec 7, 2015.

  1. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    Hey there!

    I had the chance to dive deep into Unity's event system in our current mobile game project. Two weeks ago I wrote down some basics about it.

    Yesterday I got a bit more practical/technical and described how we extended the input system with a generic drag & drop framework which makes it easy to add drag objects and drop targets (including a fully working code sample and pizza!).

    http://unity-coding.slashgames.org/extend-unitys-eventsystem-drag-drop/

    Amongst other things it was necessary to add a new Unity input event and to derive a custom input module from the base PointerInputModule class.

    Hope you'll find some interesting information, feel free to give me some feedback! :) I plan to do some more posts what to do with the event system, so any questions and suggestions for topics are welcome.
     
    eses likes this.
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Interesting! Thanks for doing this.

    FWIW, it have cross posted this in "teaching" with a redirect.

    BTW: What was it that wasn't working using the default/vanilla version of drag and drop in the current UI Toolset? I didn't quite understand what exact problem you were fixing.
     
  3. coeing

    coeing

    Joined:
    Mar 17, 2011
    Posts:
    271
    Hi Adam!

    The current drag events are fine and we still use them. The drag & drop operations just go one step further and give a framework to define objects from which some data can be dragged to other objects where some specific data can be dropped.

    E.g. in an inventory you may have a lot of items in your bag that will add some ItemDragData to the drag operation, when the user starts to drag an item. Your equipment slots may only accept specific items, e.g. your head only accepts items of type "helmet". This can be easily implemented by using the "StartDrag" (DragOperationHandler), "DragOver" and "Drop" (DropOperationHandler) events.

    So the developer only needs to add those game-specific checks and gets the success/fail events automatically ("DropWillBeSuccessful", "DropWillBeUnsuccessful", "SuccessfulDragDrop", "UnsuccessfulDragDrop").
     
    theANMATOR2b likes this.
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Cool. I'll have to look more closely at this. I've done a little D&D (not the Dave Arneson and Gary Gygax kind) and, yes, there was the need to keep track of DragItems and DropTargets and other data.

    Thanks.
     
    coeing likes this.