Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

IBeginDragHandler, IDragHandler, IEndDragHandler script attached to Sprite object

Discussion in 'Scripting' 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
    ALIENPANDA likes this.
  2. Leo-Yaik

    Leo-Yaik

    Unity Technologies

    Joined:
    Aug 13, 2014
    Posts:
    435
    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:
    6
    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
    parisazarei7, SiWoC, ahodge and 8 others like this.
  5. dustin_pau1

    dustin_pau1

    Joined:
    Dec 18, 2015
    Posts:
    11
    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,712
    This is a forum, and there are not "Right" tags or anything like that. :)
     
  7. kthrose

    kthrose

    Joined:
    Mar 19, 2019
    Posts:
    5
    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.
     
    VARLab_CC and breylinlee like this.
  8. Ma-O

    Ma-O

    Joined:
    Apr 21, 2018
    Posts:
    1
    One more thing to note, you must implement IDragHandler in order for BeginDrag and EndDrag to work
    You can implement Dragging on its own, but the head and tail requires the body or it won't work, so to speak.
    Took me a few hour to figure that out :/
     
  9. FernandoDarci

    FernandoDarci

    Joined:
    Jan 25, 2018
    Posts:
    19
    In fact, most of all things in Unity are far more complicated that needs.

    if you are using UI elements (inside Canvas), there´s the Mouse.InputPosition like the only thing you need.
    OnPointerDown sticks the element to the MousePosition, so you can move it freely. So, the next step is the target position, that can be done with a single RectTransform and a MouseEnter/MouseExit events. Putting all toghether,
    if the mouse enters on the targetPosition and is carrying the source element, onPointerUp simply "Snaps" the source, giving the target transform to the source transforms.

    So you have a source canvas element (I strongly recommend use Image instead of SpriteRenderer) with IPointerDownHandler and IPointUpHandler and a target canvas element (that can be anything with a rectTransform attached to it) with IPointerEnterHandler and IPointerExitHandler.

    OnPointerDown -> source sticks to mouse position.
    OnPointerUp -> If source is on target, snaps source to target, else do whatever you think (drop the item in scene, return it to source position, leave where it is, etc...)

    OnPointerEnter -> tells source that is on target;
    OnPointerEnter -> tells source that is not on target;

    More easy than this, only if you pay someone to do.

    Edit: If you really need a SpriteRenderer in your scene, put it as a child of an empty object with RectTransform.
     
  10. DarkGate

    DarkGate

    Joined:
    Jan 26, 2016
    Posts:
    33
    Why are there so many different interfaces for the same things? IDrag, OnPointer, so very confusing. And the setup instructions are terribad, this is why its frustrating to setup something that should be simple. I've used unity for YEARS and still having trouble setting something so simple up. Instead of acquiring other companies, how about we focus on making the core Unity product better for the users?
     
    spqrpancho likes this.
  11. parisazarei7

    parisazarei7

    Joined:
    Mar 7, 2018
    Posts:
    3
    Thanks alot