- Home /
Question by
alexjolig · Jul 24, 2016 at 08:41 AM ·
unity5drag-and-drop
How do I detect OnDrop event on a GameObject?
I need to implement drag & drop system in my project which I did using this code.
using UnityEngine; using System.Collections;
public class DragDrop : MonoBehaviour {
private Vector3 offset;
void OnMouseDown()
{
offset = gameObject.transform.position -
Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 4f));
}
void OnMouseDrag()
{
Vector3 newPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 4f);
transform.position = Camera.main.ScreenToWorldPoint(newPosition) + offset;
}
}
Now I need to implement OnDrop event like the one we use in canvas.
public class Slot : MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData eventData)
{
Debug.Log("Somthing Dropped!");
}
}
As far as I know I can't use IDragHandler events out of canvas, so Is there anyway to detect OnDrop event on a GameObject like the above code?
Comment