Unity Drag & Drop Slots-Inventory

Many of you seasoned unity pros out there are probably face-palming as you look at how I’ve approached this, but this is the method that is working for me at this point. I’m sure it will be revised in the future.

The script below is attached to all of my quickslot UI images. I use an extra “phantom” slot (dragImage & dragText) to play the part of the slot that is being dragged. so the actual slots or slot items don’t move, the slot data is just passed behind the scenes. If you have any q’s  you can contact me dev@nuzly.com. otherwise, i hope this is useful in some way.

Also, check out this tutorial for a good run down on a variation of the code below. It’s the tutorial that clued me into this method, also check the unity forums for some useful unity 4.6 > 5 info. (which is where i got the UI raycasting stuff.)

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DragHandler : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler {

    public GameObject dragObject;
    public Image dragImage;
    public Sprite emptyImage;
    public Text dragText;
    public int slot;
    SlotManager sm;
    Vector3 startPos;

	void Start () {

        dragObject = GameObject.Find("Slot_Drag");
        dragImage = GameObject.Find("Slot_Drag_Image").GetComponent();
        dragText = GameObject.Find("Slot_Drag_Counter").GetComponent();

        sm = GameObject.Find("InventoryUIManager").GetComponent();
	}
	

    public void OnBeginDrag(PointerEventData eventData)
    {
     
        dragImage.overrideSprite = sm.GetSlotSprite(slot);
        dragText.text = sm.GetSlotCountText(slot);
        startPos = dragObject.transform.position;

        sm.slotImages[slot].GetComponent().overrideSprite = emptyImage;
        sm.slotCounters[slot].GetComponent().text = "";

    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector3 m = Input.mousePosition;
        dragObject.transform.position = new Vector3(m.x, m.y, dragObject.transform.position.z);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        CheckDrop();
      
        dragObject.transform.position = startPos;
        sm.UpdateSlots();

        dragImage.overrideSprite = emptyImage;
        dragText.text = "";
    }

    void CheckDrop() 
    {
        int dropSlot=-1;
        PointerEventData pointer = new PointerEventData(EventSystem.current);
        pointer.position = dragObject.transform.position;
        List raycastResults = new List();
        EventSystem.current.RaycastAll(pointer, raycastResults);

        if (raycastResults.Count > 2)
        {
      
            string slotname = raycastResults[2].gameObject.name;

            if (slotname.IndexOf("Slot_01") > -1) { dropSlot = 1; }
            if (slotname.IndexOf("Slot_02") > -1) { dropSlot = 2; }
            if (slotname.IndexOf("Slot_03") > -1) { dropSlot = 3; }
            if (slotname.IndexOf("Slot_04") > -1) { dropSlot = 4; }
            if (slotname.IndexOf("Slot_05") > -1) { dropSlot = 5; }
            if (slotname.IndexOf("Slot_06") > -1) { dropSlot = 6; }
            if (slotname.IndexOf("Slot_07") > -1) { dropSlot = 7; }
            if (slotname.IndexOf("Slot_08") > -1) { dropSlot = 8; }
            if (slotname.IndexOf("Slot_09") > -1) { dropSlot = 9; }
        
           
        }

        if (dropSlot != -1) 
        {
            sm.SetDragFrom(slot);
            sm.SetDragTo(dropSlot-1);

        }

        Debug.Log(dropSlot);
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s