Creating a Drag and Drop Spell Bar

Implementing a Drag and Drop Spell Bar: by Mike Cook.

Description:
I am sure this has been covered somewhere in the Unity Forums, but I couldn't find any relevant information when I was starting out on this, so hopefully this tutorial will help someone starting out with Unity. Basically we will be mimicking the spell bar seen in such games as World of Warcraft, or Aion.

Note:
''I discovered this incomplete tutorial and found value to it. It's worth a test and a read-thru.''

''However, I have since found that many of the methods that are described in this tutorial can be found in the existing Unity "events" methods. These "events" are poorly documented, unfortunately. One should read this tutorial, and then look thru the docs for additional information on Events (http://unity3d.com/support/documentation/ScriptReference/Event.html) and EventTypes (http://unity3d.com/support/documentation/ScriptReference/EventType.html). These can include "if (myCurrentRect.Contains(Event.current.mousePosition)) {}" and "if (Event.current.type == EventType.MouseDrag) {}" and "if (Event.current.delta.magnitude > myDeltaThreshold) {}". Often it is recommended that one save the current event as: "var currentEvent : Event = Event.current;" and then use currentEvent for the rest of the method to avoid asking for Event.current repeatedly."'' - Little Angel

Step 1: Plan what you want to do before you do it
I have a very bad habit of "rushing" into things. I get an idea, and I HAVE TO IMPLEMENT IT THAT VERY SECOND. So I quickly slap things together in my main script and it usually results in a mess. I can't stress enough the fact that careful planning will go a long way. For our custom drag and drop spell bar, we want to implement the following features:  Easily add items to the spell bar. Drag and drop items off spell bar to delete them. Drag and drop item into empty slot to switch its location. Drag and drop item into occupied slot to swap the items. Restrict certain items from going into certain slots. Drag and drop feature will be "sticky" meaning the user will have to drag a ways for the icon to disengage from the slot. Clicking an item will activate it, or use it. Right clicking while dragging will cancel the currently dragged item.  Unity out of the box does not support any button that will accomplish what we want to do (that I am aware of). The default GUI.Button only registers when you release the button, and the Repeat Button only registers when the user is holding the mouse down while above the button. It became clear when I first started this project that I would have to implement my own buttons and ditch Unity's all together.

We actually only need to know 2 things to implement this drag and drop system:  Where the mouse is when the user presses the mouse button down. Where the mouse is when the user releases the mouse button. 

Thats pretty much it. It will get a little more complicated then that, but those are the key points to get the drag and drop system to work. One of the hardest parts of this script was "knowing where the mouse is" at all times. I struggled with different ways to loop through different Rect's and try to figure out where the mouse was at. Then I had an idea, what if we check if the mouse is inside a button, as we draw it? It is definitely the way to go, and so we will create our own function to draw each icon that records if the mouse is inside it or not.

Ok so on to the rest of the tutorial. If you haven't yet learned these features, you will know them pretty well by the time we are done. For our drag and drop system we will be using:  Classes <li>Multi-Dimensional Arrays. <li>Passing variables by Reference (kinda similar to pointers in C++) </ul> Sounds confusing and scary doesn't it? It's not, just bear with me and I will explain them to you as we go.

Step 2: Project Setup
First and foremost, create a new project in Unity, and chose not to import any of the standard packages. We will be working with a clean scene. I have created some test icons we can use and can be downloaded here: Drag and Drop Icons.zip

So download the zip and extract the icons. Import them into your project using Assets > Import new asset...

I like nice, clean, crisp images when working with the GUI, so click on each image and in the Insepector set the Texture Format to ARGB32, and click the apply button. Our system will rely on manually loading resources, so create a Resources folder in your project and dump all the images in there. In case you haven't done this before, if you make a Resources folder, anything you put in there you can load using Resources.Load

Step 3: Item Classes
We will be using classes for storing information about the items in the spellbar. There are different ways to work with classes. The method I am going to show you is my favorite because of its ease of use. We are going to be creating 2 item classes, the evil smile class and the fist punch class. (exciting, I know!)

To start, create 2 new javascript assets and name one classEvilSmile.js and the other classFistPunch.js. The beauty of these is you don't have to attach them to anything, as long as you create them you can access them anywhere.

Copy and paste this into the classEvilSmile.js:

And copy and paste this into the classFistPunch.js: So let's go over all the attributes. First one is the type. We set what type of item this class is. Later on, we will add restrictions to the slot bars about what items are allowed in them. They will check each item's type and if it matches then the item will be allowed to be in that slot. Next is the icon. This is the icon the item will use when it is docked in a slot. Then we have it's description. When we click on an item, it will call the use function, and we have it just printing the description of the item, but you could put any code you wanted in the use function.

Step 4: Creating the main HUD script
This script is going to be somewhat long, but I will go over each section and try to explain what is going on. Ok, so we need an empty GameObject and a script to control the HUD. Create an empty GameObject, name it something like _Setup (or whatever you want), create a new javascript and name it HUD and attach it to the GameObject. Now open the HUD script and delete everything inside and paste this in there:

Step 5: Overview
Long script eh? Ok, if you haven't already done this, go to the top of the UniSciTE editor and select View > Toggle all Folds. This will collapse each function down to a more readable format.

TO BE CONTINUED
Am going to have to continue this later. Explanations coming soon.