Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect left mouse click but not when the click occur on a UI Button component [closed]

Tags:

c#

unity3d

My script makes a GameObject move on Input.GetMouseButtonUp(0). Unfortunately, when I click on a UI Button, the move function is triggered causing the GameObject to move.

I do not want my GameObject to move when I press a button on the screen (UI element). I want to prevent GameObject from moving when the click is on a UI component such as Button? How can I remedy this? Also, I'd like to check if the mouse was clicked over specific UI elements (2 or 3 buttons)

like image 875
Dake Avatar asked Dec 10 '22 16:12

Dake


1 Answers

After reading your comment. What you need is EventSystem.current.IsPointerOverGameObject() which checks if pointer is over UI. true when pointer is over UI, otherwise false. You can use it with '!' and run your rotation code only if the mouse is not over the UI.

For Desktop

if(Input.GetMouseButtonUp(0) && !EventSystem.current.IsPointerOverGameObject())
{
    //Your code here
}

// For Mobile

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && !EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
    //Your code here
}   

The solution above should work but there is a bug. When the mouse button is pressed over the UI Button then released outside of the UI Button, it will register as a click. So, basically, the solution works only when you click on UI Button and release on UI Button. If you release outside the U Button, the original problem will be back again.

The best solution is to use a temporary boolean value and check if button was originally pressed on a UI with Input.GetMouseButtonDown. That saved boolean, you can then use when the Button is released with Input.GetMouseButtonUp(0). The solution below is provided to work with both Mobile and Desktop. Mobile tested and it works.

bool validInput = true;

private void Update()
{
    validateInput();

    #if UNITY_STANDALONE || UNITY_EDITOR

    //DESKTOP COMPUTERS
    if (Input.GetMouseButtonUp(0) && validInput)
    {
        //Put your code here
        Debug.Log("Valid Input");
    }
    #else

    //MOBILE DEVICES
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && validInput)
    {
        //Put your code here
        Debug.Log("Valid Input");
    }
    #endif
}

void validateInput()
{
    #if UNITY_STANDALONE || UNITY_EDITOR
    //DESKTOP COMPUTERS
    if (Input.GetMouseButtonDown(0))
    {
        if (EventSystem.current.IsPointerOverGameObject())
            validInput = false;
        else
            validInput = true;
    }
    #else
    //MOBILE DEVICES
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
        if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            validInput = false;
        else
            validInput = true;
    }
    #endif
}
like image 73
Programmer Avatar answered Apr 10 '23 18:04

Programmer