Process for Understanding the Drag and Drop System

Though I’m somewhat experienced with Unity and C#, I am not so experienced with Unity’s UI internals so I had quite a bit to learn.

  • Strategy

    • Watch the lectures

My strategy, which I created as I was doing it out of thin air, was this: I quickly watched all the lectures from Drag and Drop Scripts to Drag and Drop Challenge.

I didn’t do the challenges because I’m dealing with right-hand tendonitis (the thumb in particular; am trying to remember to use the left thumb for the space bar.) Perfect time for a course of watching and reading code and not much coding.

So, 90% went in one ear and out the other.

    • Read the Code

So, I then pulled up the code (I prefer Jetbrains Rider, but it’s not free). I then tried to understand it from reading. Now, there are levels of understanding.

E.g. you could have steps 1, 2, 3, up to say 50. You understand step 1, going from step 1 to step 2, going from step 2 to step 3, and on up to step 50. (As a mathematician, that is how I first read a proof.) You understand all the parts but can’t quite grasp it as a single entity, or what psychologists call “a Gestalt”.

    • Watch the lectures again

So, I rewatched part of the lectures. Specifically, I picked the ones with the “meat” of explaining the drag and drop system, namely Drag and Drop Scripts and More Dragging and Dropping.

Then the gestalt came together.

    • Aside about Math

(in math, it’s harder, I might read something over and over, put it aside, come back in a few years, put it aside, and several years later, finally get it. I just did that with Singular Homology of Algebraic Topology, from a graduate class I took back in 1991, where the prof lost me in no time, for years reread many authors, including Roger Penrose’s pictorial treatment in Road to Reality which at least gave me an intuitive understanding, and just two days ago, was reading it for the nth time from an MIT OCW lecture, and suddenly something clicked.)

  • Summary of my Understanding

So, the key for me with dragging and dropping is that the Unity event system does the hard work at a low level.

    • Unity Event Interaction

When you click the icon and start to drag, Unity automatically calls every monobehaviour that implements “IBeginDragHandler” and it calls the “OnBeginDrag” message, with an “eventData” structure passed to it.

As you drag it, every frame (I think) it calls “IDragHandler.OnDrag” passing an eventData which our code uses to get the position of the pointer and make sure the sprite is displayed with the pointer.

Then when you let go of the mouse button, “IEndDragHandler.OnEndDrag” is called. Then our code does a lot.

    • The actual moving of the data

It reverts the icons to original states, then if you had dropped over something that can accept what was dragged, it updates the sprites and counts. It tries to swap sprites between source and destination icons, and if that’s not possible, it does a simpler “null the source sprite and set the destination sprite”.

It also tries to update the number of items in the slots. If not possible (e.g. exceeded max) it tries to move “some” of the items. If that fails, it reverts all and the operation doesn’t happen.

    • Edge Cases Metaphor and an example from work

As Sam said, it handles a lot of “edge cases” (that’s a cube metaphor. The faces are the main cases, but there are all these exceptions: the edges and even worse, the corners, where more than one face will meet). Edge cases are often the less fun parts to program, and 1. easy to get wrong and, 2. easy to forget a rare case. (An engineer did that where I worked. His code worked for every integer input, except 0 which in practice, happened often. But he tested using a random number generator that never came up 0 and it took, well, me to read the code and notice it didn’t work for 0).

1 Like

Great job getting an understanding of this. I’ll be the first to admit that we didn’t do the best job of explaining the inner workings of the Drag and Drop system, and it does throw lots of students when they try to go through it.

Me too, worth every penny and then some!

Ultimately, after the complete transaction is finished, the UI will rebuild itself, so the swapping of sprites isn’t as neccessary as the next step

This is the meat of it. It’s what happens in the sources and destinations that ultimately determines the result.

There is nothing more frustrating that code that passes a Unit test and then fails in the field because the Unit test was wrong to begin with.

Privacy & Terms