Search Unity

IBeginDragHandler, IDragHandler, IEndDragHandler script attached to Sprite object

Discussion in '2D' started by Seidensticker, Jan 28, 2015.

  1. Seidensticker

    Seidensticker

    Joined:
    Dec 9, 2014
    Posts:
    3
    Hello, I could use some guidance with implementing drag handlers in a script and attaching the script to a 2D sprite object.

    Let me explain my scenario..

    I created an empty object, added a Sprite Renderer to it, and then added a Circle Collider 2D to it.
    I have the following script:
    Code (CSharp):
    1. public class DragObject : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
    2. {
    3.     void Start()
    4.     {
    5.         Debug.Log("Start");
    6.     }
    7.  
    8.     public void OnBeginDrag(PointerEventData _EventData)
    9.     {
    10.         Debug.Log("OnBeginDrag");
    11.     }
    12.  
    13.     public void OnDrag(PointerEventData _EventData)
    14.     {
    15.         Debug.Log("OnDrag");
    16.     }
    17.  
    18.     public void OnEndDrag(PointerEventData _EventData)
    19.     {
    20.         Debug.Log("OnEndDrag");
    21.     }
    22. }
    Now, when i run the project, and click & drag, none of the methods seem to be getting called. Can someone please explain why none of the drag handler's are being called? Do they not work with 2D sprite objects? Am I missing something?
     
    Last edited: Jan 29, 2015
  2. Leo-Yaik

    Leo-Yaik

    Unity Technologies

    Joined:
    Aug 13, 2014
    Posts:
    168
    Hi,

    You will need to have the following

    1. Attach a Physics 2D Raycaster to your camera
    2. Add EventSystem into your Scene (GameObject -> UI -> EventSystem)
     
  3. BagelOrb

    BagelOrb

    Joined:
    Oct 19, 2015
    Posts:
    1
    Thanks
     
  4. diegoleao

    diegoleao

    Joined:
    Oct 27, 2006
    Posts:
    23
    Turns out you also need to add a "Standalone Input Module" on your EventSystem.... I was searching the web on how to use this damn OnDrag thing, and it was a big waste of time.

    To those of you who also wasted time on this, you need to:

    0) Go to your camera and add a "Physics 2D Raycaster" (yeah I know, how would you know)

    1) In the "Hierarchy" panel, click "Create", select "UI", and create a new "EventSystem".

    2) In the "Hierarchy" panel, click "Create", select "UI", and create a new "Canvas". EDIT: If you add the Canvas first, it creates also an EventSystem for you, but if you depend on the order added you are screwed trying to understand this thing.

    3) INSIDE the Canvas, put you sprites and add a Script like the one shown above in this Thread, implementing "IBeginDragHandler, IDragHandler, IEndDragHandler".

    Then you should be able to drag........ Is it just me or this sound awfully complex to just drag something using the built in OnDrag function....?

    I read that I needed "an event system", and what I did was try to create only an empty object with an "Event System" component. However what people meant by "add an event system" was to go to UI and then select EventSystem, which also comes with a "Standalone Input Module" component built in, therefore you "don't have to worry about it". Damn it.
     
    Last edited: Jun 3, 2016
  5. dustin_pau1

    dustin_pau1

    Joined:
    Dec 18, 2015
    Posts:
    3
    I know this is old, but can someone tag this answer as the right answer. Thanks!
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    This is a forum, and there are not "Right" tags or anything like that. :)
     
  7. kthrose

    kthrose

    Joined:
    Mar 19, 2019
    Posts:
    2
    This post is legit, you are a hero. I also want to point out if you already had a canvas you custom created, and this still isn't working, it is likely because you need a Graphic Raycaster component, which comes by default with a Canvas when created via hierarchy panel.

    Another point worth clarifying - because by the time you come to this on the web, it's probably after you've tried a bunch of bad ideas - you should/need not have an EventSystem, a Physics 2D Raycaster, or a 2D Box Collider on your Image. In fact, you will save yourself a lot of headaches if you follow steps exactly as OP wrote, because Unity engine adds a bunch of extra components that won't show up when you create empty and add.