The problem is that I just ignored

public void BeginDrag() and public void OnDrag()

I'm still not doing anything inside. Only I issue messages through Debug. Log (); Already rummaged all that could be searched. Everywhere do through these 2 functions. For GUI it is used OnMouseDrag() that I, just in case, also pereproviril with UI and also zero reaction.

Canvas itself is displayed normally. Canvas buttons react to mouse hover and click. I tried to script the script as a button, or just on the panel. Reactions - zero.

Unity last.

Does anyone have any ideas?

  • And what about the cameras? How many cameras do you have on stage? Try to remove the extra ones - Mikhail Efremov
  • 1 on camera on stage. Formally, only the camera and canvas with buttons / panels. - Andrew
  • What platform? - Xumera_hZ
  • one
    And IDragHandler impliment class? Here you can see how the definition should look like - docs.unity3d.com/ScriptReference/UI.ScrollRect.OnDrag.html - Mikhail Efremov
  • @MikhailEfremov, thank you very much, the reason turned out to be exactly that. But at the same time, it’s strange, I found 3-4 ready-made scripts that worked for other people and they didn’t work for me .... In any case, the answer was the answer that I would mark as correct. Everyone else also thanks for participating! - Andrew

2 answers 2

For the BeginDrag (), OnDrag () and OnEndDrag () methods to work, you need to implement the IBeginDragHandler , IDragHandler and IEndDragHandler interfaces, respectively.

A class definition that accepts all methods will look something like this:

 using UnityEngine; using UnityEngine.EventSystems; public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public void OnBeginDrag(PointerEventData eventData) { } public void OnDrag(PointerEventData data) { } public void OnEndDrag(PointerEventData eventData) { } } 

    Alternatively, you can add an EventTrigger object to the object and assign a method call in the required script for each event. It also works great, plus it gives the ability to customize the call when it is really required.

    When the method looks like this (as an argument to BaseEventData ), in the trigger it will be available for the call.

      public void OnDrag(UnityEngine.EventSystems.BaseEventData eventData) { var pointerData = eventData as UnityEngine.EventSystems.PointerEventData; if (pointerData == null) { return; } Vector3 curPos = rect.position; curPos.x += pointerData.delta.x; curPos.y += pointerData.delta.y; rect.position = curPos; }