I have an inventory system based largely on this tutorial that I am currently in the process of modifying. OnBeginDrag
and OnEndDrag
work as expected, but OnDrag
, though continuously updating the position of the item, does not display the item. It disappears during OnDrag
, and reappears once it has been dropped in its new slot.
Here is my hierarchy:
Of note are the two cameras. The DungeonGroup
(and the DungeonUICanvas
) use DungeonCamera
as far as I can tell. (DungeonGroup
was, until recently, a completely different Scene. The OnDrag
worked as expected before I integrated the scenes, making me think that a camera issue could be the culprit.)
Here is the code I am currently working with:
public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public static GameObject item; //itemBeingDragged
public static Vector3 startPosition;
public static Transform startParent;
public void OnBeginDrag(PointerEventData eventData)
{
item = gameObject;
startPosition = transform.position;
startParent = transform.parent;
GetComponent<CanvasGroup>().blocksRaycasts = false;
transform.SetParent(transform.root);
}
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
item = null;
if (transform.parent == startParent || transform.parent == transform.root)
{
transform.position = startPosition;
transform.SetParent(startParent);
}
GetComponent<CanvasGroup>().blocksRaycasts = true;
}
}