UGUI: Buttons on scrollview don't always register touches
I am building a UI for mobile using UGUI
.
This is how buttons normally work:
You press down on the button, registers a pointer down event.
You let go of the button, registers a pointer up event.
IF pointer was hovering button on last event, register click event.
.
This is how buttons work when on top of some component that steals drag events (like a scrollview)
You press down on the button, registers a pointer down event.
If you move your mouse just a single pixel, registers a pointer up event.
.
This isn't a problem on PC because it's not hard to keep your mouse still while pressing the button.
On mobile, however, we are facing some issues. The user has to be very fast/precise when pressing buttons or they will not register their click events. I tried firing the button events on pointer up events instead, but then it will fire immediately on the slightest registered drag.
.
I think this has something to do with the underlying scrollview stealing the pointer event focus, so the button has to terminate its events early.
.
What do you think? Do you have any solutions for this?
Answer by Siccity · Nov 28, 2019 at 11:57 AM
Okay so I figured it out.
The EventSystem component has a value called DragThreshold. This value determines how far you have to drag before the event turns into a valid drag action.
The problem here is that this threshold is in pixels. This causes issues on High-DPI devices.
The solution is to modify this value with respect to the device's DPI.
I found this script online which solves the issue:
/// <summary> Fixes DragThreshold on high-dpi devices </summary>
public class DragThresholdUtil : MonoBehaviour {
private void Start() {
int defaultValue = EventSystem.current.pixelDragThreshold;
EventSystem.current.pixelDragThreshold = Mathf.Max(defaultValue, (int) (defaultValue * Screen.dpi / 160f));
}
}
Your answer
Follow this Question
Related Questions
How to create a scrolling menu that scales to all screen sizes 2 Answers
Hotbar appears when a certain gameobject is selected? 1 Answer
Unity Button reacts even if Canvas is disabled 0 Answers
Do Buttons dirty the canvas? 3 Answers
How to hide x number of button while the button its doing something.. 0 Answers