unity3d Input System Read Mouse Button ( Left, Middle, Right) Clicks


Example

These functions are used to check Mouse Button Clicks.

  • Input.GetMouseButton(int button);
  • Input.GetMouseButtonDown(int button);
  • Input.GetMouseButtonUp(int button);

They all take the-same parameter.

  • 0 = Left Mouse Click.
  • 1 = Right Mouse Click.
  • 2 = Middle Mouse Click.

GetMouseButton is used to detect when mouse button is continuously held down. It returns true while the specified mouse button is being held down.

void Update()
{
    if (Input.GetMouseButton(0))
    {
        Debug.Log("Left Mouse Button Down");
    }

    if (Input.GetMouseButton(1))
    {
        Debug.Log("Right Mouse Button Down");
    }

    if (Input.GetMouseButton(2))
    {
        Debug.Log("Middle Mouse Button Down");
    }
}

GetMouseButtonDown is used to detect when there is mouse click. It returns true if it is pressed once. It won't return true again until the mouse button is released and pressed again.

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Debug.Log("Left Mouse Button Clicked");
    }

    if (Input.GetMouseButtonDown(1))
    {
        Debug.Log("Right Mouse Button Clicked");
    }

    if (Input.GetMouseButtonDown(2))
    {
        Debug.Log("Middle Mouse Button Clicked");
    }
}

GetMouseButtonUp is used to detect when the specififed mouse button is released. This is will only return true once the specified mouse button is released. To return true again, it has to be pressed and released again.

void Update()
{
    if (Input.GetMouseButtonUp(0))
    {
        Debug.Log("Left Mouse Button Released");
    }

    if (Input.GetMouseButtonUp(1))
    {
        Debug.Log("Right Mouse Button Released");
    }

    if (Input.GetMouseButtonUp(2))
    {
        Debug.Log("Middle Mouse Button Released");
    }
}