Hi all, i am using unity’s new ui to implement drag and drop feature for learning.The problem i am facing is that when i drag a image out of placeholder and drop on empty space it sticks to that as in image.
But if i drag another image and drop it on above any five slots, it successfully placed.Also if i swap two images, one goes to below of that image.Like this,
What i am trying to do is to implement like card games like solitaire.Here is the code.
This script goes to image objects like in A, B etc.
public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public static GameObject itemBeingDragged;
Vector3 startPosition;
Transform startParent;
#region IBeginDragHandler implementation
public void OnBeginDrag (PointerEventData eventData)
{
itemBeingDragged = gameObject;
startPosition = transform.position;
startParent = transform.parent;
GetComponent<CanvasGroup> ().blocksRaycasts = false;
}
#endregion
#region IDragHandler implementation
public void OnDrag (PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
#endregion
#region IEndDragHandler implementation
public void OnEndDrag (PointerEventData eventData)
{
itemBeingDragged = null;
if(transform.parent != startParent)
{
transform.position = startPosition;
}
GetComponent<CanvasGroup> ().blocksRaycasts = true;
}
#endregion
}
this script goes to placeholders
public class Slot : MonoBehaviour, IDropHandler
{
public GameObject item
{
get
{
if(transform.childCount > 0)
{
return transform.GetChild(0).gameObject;
}
else
{
return null;
}
}
}
#region IDropHandler implementation
public void OnDrop (PointerEventData eventData)
{
if(!item)
{
DragHandler.itemBeingDragged.transform.SetParent(transform);
}
}
#endregion
}