- Home /
OnDrag methods not being called on Android in Unity 2018.3.5f1
I had the following kind of UI based player's controller code for an Android phone, and in Unity 2017.4.18f1 it worked as expected. In Unity 2018.3.5f1, none of the OnDrag
methods get called, while OnPointer*
methods do get called. Inside the editor in Windows using a mouse pointer to simulate touch controls, all of them still get called. Is this a bug in Unity 2018? Need help...
public class VrJoypedAxis : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerUpHandler, IPointerDownHandler
{
...
void Start()
{
...
}
public void OnBeginDrag(PointerEventData ped)
{
...
}
public void OnDrag(PointerEventData ped)
{
...
}
public void OnEndDrag(PointerEventData ped)
{
...
}
public void OnPointerDown(PointerEventData ped)
{
}
public void OnPointerUp(PointerEventData ped)
{
...
}
}
UPDATE:
I tried alternative way, so this time I used "Event Trigger" component on an UI image object, with three events, pointer up, pointer down and drag. Alternative script looks like this::
public class VrJoypedAxis : MonoBehaviour
{
public void Dragging() //Attached to Event Trigger component -> Drag
{
...
}
public void ClickDown() //Attached to Event Trigger component -> Pointer Down
{
...
}
public void ClickUp() //Attached to Event Trigger component -> Pointer Up
{
...
}
}
And it doesn't work in exactly the same way as code above. With mouse cursor in Unity editor, all events work in both Unity 2017 and Unity 2018. On android device using touch input, in Unity 2017 all events also work but in Unity 2018, only pointer up and pointer down events work, while drag is dead.
As it looks to me, drag events are completely busted for touch input in Unity 2018.3.5f1.
Answer by Siegewolf · May 04, 2019 at 09:54 PM
Finally found the problem.
Cursor.lockState = CursorLockMode.Locked;
As of Unity 2018/2019, this command is disabling OnDrag
events for touchscreen input when using Unity UI system. It might be doing the same not just in case of UI.
Don't know if it is a bug or intentional fix in Unity 2018/2019 (make somewhat sense that it is intentional), but once I remove this command, drag is working fine once again.
Thank you for this. Don't know how much time you just saved me lol
Saved me man.. I have been struggling for days and now i find out that a line from an unrelated script a made same time ago used this lockstate.. Thank you sir
@Siegewolf $$anonymous$$y project was working fine on Unity 5, now on 2019 got this problem.. I saw your reply but i just don't understand where to find this code to change? " Cursor.lockState = CursorLock$$anonymous$$ode.Locked;"
That line of code was in one of my scripts. It may be in one of yours too, if it is the same problem.
Unfortunately no, i looked through the whole thing did not find any. I genuinely started to lose hope solving this, as i literally read every single post about this problem and i still have it. I even posted question on Reddit and Stackoverflow got 0 solutions.
If using the legacy FPSController from Standard Assets, you can disable it in the Inspector, First Person Controller > $$anonymous$$ouse Look > Lock Cursor
Answer by dma8 · Feb 16, 2019 at 01:09 AM
I was struggling with the same problem on iOS. Try setting the Max Ray Intersections to zero on your Physics Raycaster. I can't test on Android but hopefully this will work for you also. Apparently there is bug with the Physics Raycaster. See the post below.
The thing is, my virtual controller is based on Unity's UI system. This system is using Graphics Raycaster, not Physics or Physics2D Raycaster component. Graphics Raycaster is assigned as component to UI canvas by default when canvas is created. And according to Unity manual, Graphics Raycaster is the one that needs to be used with Unity's UI system and this one doesn't have $$anonymous$$ax Ray Intersections variable.
Regardless of this, I tried now to swap Graphics Raycaster with Physics one in UI canvas, but as expected, now whole UI is non-intractable even in Unity editor.
Answer by alihaghshenas821 · Mar 16, 2021 at 11:38 PM
Cursor.lockState = CursorLockMode.Locked;
this didnt solve my problem and i couldnt find anything that show me a way to use IDragHandler in android i couldnt use EventTrigger cus i needed the dragging distance
actually i realized that i dont need dragging for what i need in my game but this might solve your problem if you are struggling with this issue like me
public class AttackButtonCameraMove : EventTrigger
{
[SerializeField] private FixedTouchField _touchField;
private void Awake()
{
_touchField = FindObjectOfType<FixedTouchField>();
}
public override void OnPointerDown(PointerEventData data) {
_touchField.attackPuttonPressed = true;
_touchField.PointerOld = data.pressPosition;
_touchField.Pressed = true;
}
public override void OnPointerUp(PointerEventData data)
{
_touchField.Pressed = false;
_touchField.attackPuttonPressed = false;
}
}
you can simply override OnDrag as well and it will work on android