This question already has an answer here:
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
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!