-1

How To Dragging Item With Icon Following Pointer ?

MAYBE IT WILL TAKE SOME TIMES TO SEE ALL THE CODE. Many thanks Before for your attention.

But I have done a little Code Before. And it is run successful, but there is some mistake in my code. Actually the icon dragging is following the pointer but it just temporary.

And my prediction is because i make an paging inventory Storage 1 , 2 , and 3. When All of 3 Storgae is visible the dragging item icon run successfull with following the pointer. But if one of the inventory storage was hide for example : i have click storage 2 then storage 1 and 3 are hide then when i dragging the item the icon was not following the pointer. It become Stuck. But still can swap item slot. Just only the icon not following the pointer.

Below is my code it a little long :) I will give the detail code.

slotScript.cs (this script is a prefabs of Slot that show item icon, amount)

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

public class slotScript : MonoBehaviour, IPointerDownHandler, IPointerEnterHandler, IPointerExitHandler, IDragHandler {
    Image itemImage;
    Sprite icon;
    Text amount;
    public int slotNumber;
    inventory inventoryx; 
    player Playerx;
    item temp;
    menu menux;
    dragitemicon dragger;

    // Use this for initialization

    void Start () {
        inventoryx = GameObject.FindGameObjectWithTag ("inventory").GetComponent<inventory> ();
        itemImage = gameObject.transform.GetChild (0).GetComponent<Image> ();
        amount = gameObject.transform.GetChild (1).GetComponent<Text> ();

    }

    // Update is called once per frame

    void Update () {
        //Show Images Icon Slot Inventory

        if (inventoryx.Player.items[slotNumber].itemName !=  null) {
            itemImage.enabled = true;
            icon = Resources.Load<Sprite> (inventoryx.Player.items[slotNumber].itemIcon + "/" + inventoryx.Player.items[slotNumber].itemName);
            itemImage.sprite = icon;

            amount.text = inventoryx.Player.items[slotNumber].itemStock.ToString();
            amount.enabled = true;

        } else {
            itemImage.enabled = false;
        }


    }

public void OnPointerDown (PointerEventData data) {
        if (inventoryx.Player.items[slotNumber].itemType == item.ItemType.Raw) {
            inventoryx.Player.items [slotNumber].itemStock--;
        }
        //Debug.Log (transform.name);
        if (inventoryx.Player.items [slotNumber].itemName == null && inventoryx.draggingitem) {
            inventoryx.Player.items [slotNumber] = inventoryx.getdragitem;
            inventoryx.closeDragItem ();
            //inventoryx.dragitemicon.GetComponent<Image>().sprite = null;
        } else if (inventoryx.draggingitem && inventoryx.Player.items[slotNumber].itemName != null ) { 
            inventoryx.Player.items[inventoryx.indexofdragitem] = inventoryx.Player.items[slotNumber];
            inventoryx.Player.items[slotNumber] = inventoryx.getdragitem;
            inventoryx.closeDragItem ();
        }
    }

    //On Drag Close ToolTips and Show Drag Item
    public void OnDrag (PointerEventData Data) {
        if (inventoryx.Player.items[slotNumber].itemType == item.ItemType.Raw) {
            inventoryx.Player.items[slotNumber].itemStock++;
        }
        if (inventoryx.Player.items[slotNumber].itemName != null) {
            inventoryx.showDragItem(inventoryx.Player.items[slotNumber], slotNumber);
            dragger.showDragItem(inventoryx.Player.items[slotNumber], slotNumber);
            inventoryx.Player.items[slotNumber] = new item();
            inventoryx.closeToolTips();
            amount.enabled = false;

        }
    }
}

How to change dragging code above i used to this code below :

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

    public void OnBeginDrag(PointerEventData eventData) {}

    public void OnDrag(PointerEventData eventData) {
        //Debug.Log ("OnDrag");
        transform.position = eventData.position;
        }

    public void OnEndDrag(PointerEventData eventData) {}
}

Many Thanks

Dennis

Dennis Liu
  • 303
  • 5
  • 21

1 Answers1

0

Very fortunately, there is now an

incredibly simple way

to do this in Unity.

Here it is: https://stackoverflow.com/a/37473953/294884

Ask if you have more trouble!

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Hi, @Joe, Could explain this code what use for ? ( public void OnBeginDrag(PointerEventData eventData) {} ) and this ( ublic void OnEndDrag(PointerEventData eventData) {} ). Thanks – Dennis Liu Jul 02 '16 at 14:34
  • Hi Dennis! Do you mean "what is the ghost" ? – Fattie Jul 02 '16 at 14:46
  • I suggest you just put the code on your UI objects - and it all works magically! Regarding the ghost, **simply don't use it** at first, don't worry about it. It's just to make it look good as you drag. – Fattie Jul 02 '16 at 14:47
  • Hi @joe, see my code above. Thanks :) – Dennis Liu Jul 02 '16 at 14:48
  • HI @Joe, actually what i want is when i drag the item i can put it to other slot. Do you get what i mean ? I just try you code draging. It work but not really what i mean Joe. – Dennis Liu Jul 02 '16 at 16:38