Hello everyone,
This is my drag and drop scripts. How to add swap feature here? I tried different approach and searched on Google but didn’t find the solution.
Script on draggable items:
public class DragDrop : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
private RectTransform rectTransform;
private CanvasGroup canvasGroup;
// public Transform onBeginDragParent;
// private Transform onEndDragParent;
private void Awake()
{
rectTransform = GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
rectTransform.localPosition = Vector3.zero;
}
public void OnBeginDrag(PointerEventData eventData)
{
canvasGroup.blocksRaycasts = false;
// onBeginDragParent = transform.parent;
transform.SetParent(transform.parent.parent.parent.parent.parent.parent, false);
}
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
canvasGroup.blocksRaycasts = true;
rectTransform.localPosition = Vector3.zero;
}
}
Script on drop slot:
public void OnDrop(PointerEventData eventData)
{
GameObject droppedObject = eventData.pointerDrag;
if(droppedObject != null && transform.childCount == 0)
{
droppedObject.transform.SetParent(transform, false);
PlayerPrefs.SetString("Slot"+name, transform.GetChild(0).name);
}
}
Update: The main problem here is OnDrop not calling when I try to swap objects.
Hey!
I think one way would be to save the origin position or parent of your Dragged item inside it. Ex. when you place it first time you would create:
class Info : MonoBehaviour
{
public Transform originParent;
private void Start()
{
originParent = transform.parent;
}
}
Next in your OnDrop you could use it to move the object that is being dropped on to move to that position
public void OnDrop(PointerEventData eventData)
{
GameObject droppedObject = eventData.pointerDrag;
if(droppedObject != null && transform.childCount == 0)
{
(...)
thisDroppedOnObject.SetParent(droppedObject.GetComponent<Info>().originParent, false);
droppedObject.GetComponent<Info>().originParent = thisDroppedOnObject.GetComponent<Info>().originParent;
thisDroppedOnObject.GetComponent<Info>().originParent = transform;
}
}
*the code might be off. It’s just to give you an idea.
Basically the idea is to save the data about the originParent in some place and modify it when you swap it so it is always up to date for the future swapping.
I hope it helps!
1 Like
SunnyValleyStudio:
Hey!
I think one way would be to save the origin position or parent of your Dragged item inside it. Ex. when you place it first time you would create:
class Info : MonoBehaviour
{
public Transform originParent;
private void Start()
{
originParent = transform.parent;
}
}
Next in your OnDrop you could use it to move the object that is being dropped on to move to that position
public void OnDrop(PointerEventData eventData)
{
GameObject droppedObject = eventData.pointerDrag;
if(droppedObject != null && transform.childCount == 0)
{
(...)
thisDroppedOnObject.SetParent(droppedObject.GetComponent<Info>().originParent, false);
droppedObject.GetComponent<Info>().originParent = thisDroppedOnObject.GetComponent<Info>().originParent;
thisDroppedOnObject.GetComponent<Info>().originParent = transform;
}
}
*the code might be off. It’s just to give you an idea.
Basically the idea is to save the data about the originParent in some place and modify it when you swap it so it is always up to date for the future swapping.
I hope it helps!
Thank you. Actually I forgot to mention the main problem I am facing is, OnDrop is not calling when I try to swap.