Then I use button.OnClick().AddListerner() to attach a function when the button is clicked.
The problem is, everytime when I finish the drag, if the mouse up position is in the button region, the button would call the OnClick() function too. How can I disable OnClick() when it’s a just a drag action?
If your drag and OnClick functions are in the same script, you can add a bool variable that is set to true when on drag is called and set back to false when mouse button is released.
It should look like this:
I had this problem with a draggable component because that object had to be at the same time clickable but when I was dragging the component the onClick listener also was executing when the drag ends, I did this to fix that:
public bool isClickable = true;
void Start{
GetComponent<Button>().onClick.AddListener(() => executeThis(isClickable));
}
void ExecuteThis (bool isClickable){
if( isClickable){
// Do something
}
}
public void OnBeginDrag(PointerEventData eventData)
{
isClickable = false;
}
public void OnEndDrag(PointerEventData eventData)
{
isClickable = true;
}