- Home /
ScrollRect and IDragHandler on touch devices
I'm working on an iOS game, and I'm having issues with figuring out how to handle a particular situation. On the left side of the screen is my "Card List" Which is essentially an inventory list, populated according to the Scrollable List tutorial from Unity. In the center is a set of Equipment Slots, basically UI Panels with a grid layout that snaps a card to that slot. (from this tutorial) I want the user to be able to both scroll the list on the left by swiping up/down, and able to drag cards onto their equipment slots by long pressing the card, and then dragging it over.
Relevant Code: CardDragHandler.cs (attached to card, only relevant functions shown):
public void OnBeginDrag (PointerEventData eventData)
{
//transform.SetParent (PositionsPanel);
ObjectBeingDragged = gameObject;
startPosition = transform.position;
GetComponent<CanvasGroup> ().blocksRaycasts = false;
startParent = transform.parent;
PressBegan = Time.time;
DragStartPosition = eventData.position;
}
public void OnDrag (PointerEventData eventData)
{
PressTime = Time.time - PressBegan;
float distance = Vector3.Distance (DragStartPosition, eventData.position);
if(PressTime>LongPressTime && distance<1)
transform.position = eventData.position;
}
public void OnEndDrag (PointerEventData eventData)
{
ObjectBeingDragged = null;
GetComponent<CanvasGroup>().blocksRaycasts = true;
//transform.SetParent (CardContentPanel);
if (transform.parent == startParent) {
transform.position = startPosition;
}
}
CardDropHandler.cs (attached to slot):
public void OnDrop (PointerEventData eventData)
{
CardDragHandler.ObjectBeingDragged.transform.SetParent (transform);
}
How do I keep the draghandler from overriding the scrollRect? How do I kick it out of the drag event entirely so that it scrolls instead?
Answer by nieyoub23 · Dec 02, 2017 at 05:33 AM
Did you figure it out? I am trying to do the same thing but cant get them to work together.