0
\$\begingroup\$

There is a code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class DragAndDropGame : MonoBehaviour, IBeginDragHandler, IDragHandler
{

    Camera MainCamera;

    void Awake()
    {
        MainCamera = Camera.allCameras[0];
        Debug.Log(MainCamera);
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("OnBeginDrag");
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector3 newPos = MainCamera.ScreenToWorldPoint(eventData.position);
        newPos.z = 0;
        transform.position = newPos;
    }

    void OnMouseDown()
    {
        Debug.Log("OnMouseDown");
    }
}

OnMouseDown fulfills, but the implementation ofIBeginDragHandler, IDragHandler - not. What can be wrong?

\$\endgroup\$
  • 1
    \$\begingroup\$ Do you have an EventSystem set up in the scene with a Physics2DRaycaster and a 2D Collider set up on the sprite? \$\endgroup\$ – Ed Marty Aug 24 '18 at 12:04
  • \$\begingroup\$ I have sprites on 3D plane in AR. I tryed all except 2D Collider. \$\endgroup\$ – Timur Musharapov Aug 24 '18 at 12:09
  • 1
    \$\begingroup\$ You'll need the EventSystem, a PhysicsRaycaster, and a collider on the sprite for those methods to work. \$\endgroup\$ – Ed Marty Aug 24 '18 at 15:07
4
\$\begingroup\$

IDragHandler (and its associates) are only called for UI elements that are part of a canvas. And even then, only for UI components (Image, text, etc) and not 3D objects and colliders.

You are probably looking for OnMouseDrag.

\$\endgroup\$
0
\$\begingroup\$

Try adding this namespace:

using UnityEngine.UI
\$\endgroup\$
  • \$\begingroup\$ It did not help, but elements is sprites. \$\endgroup\$ – Timur Musharapov Aug 24 '18 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.