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
}


Perhaps you simply need to check, when the item is dropped, if the mouse is in fact over a SLOT (as opposed to say a PANEL, or nothing). If not, return to original position. Or am I missing that logic somehow? Also, after you do transform.SetParent, don't you need to set the child's transform to 0,0 (or half_slot_size_x,half_slot_size_y)- to get he proper offset from the parent?
– GlurthI've seen this code before :). The code looks identical to mine, so I think its likely to be a set up problem in the scene, rather then a code problem. Is the set up on all of the slots the same? I'm puzzled as to why it would work with the top slots but not the bottom ones You might be able to fix the going behind issue adjusting the z position on your objects with the DragHandler script.
– Kiwasino..on both top and bottom slots, this didnt work...yeah i have been following your youtube video to learn unity ui.Thanks..let me check scene.
– kurukshetran