Mouse Input Functions in Unity
Notes:
Unity Scripting API - Mouse Input Functions:
- in Unity we have variety of static properties & methods as part of the Input class
- they are used to detect different status of a given mouse button
Note: integer
- 0 : indicates left mouse button
- 1 : indicates right mouse button
- 2 : indicates middle mouse button
Syntax:
public static bool GetMouseButtonDown(int mouseButton)
- returns true as soon as a given mouse button is pressed
public static bool GetMouseButton(int mouseButton)
- returns true as long as a given mouse button is held down
public static bool GetMouseButtonUp(int mouseButton)
- returns true as soon as a given mouse button is released
Example Code:
using UnityEngine;
public class InputController : MonoBehaviour {
void Update ()
{
// Left mouse button Input
if (Input.GetMouseButtonDown (0)) {
Debug.Log ("Left mouse button is pressed");
} else if (Input.GetMouseButton (0)) {
Debug.Log ("Left mouse button is held down");
}else if (Input.GetMouseButtonUp (0)) {
Debug.Log ("Left mouse button is released");
}
// Right mouse button Input
if (Input.GetMouseButtonDown (1)) {
Debug.Log ("Right mouse button is pressed");
} else if (Input.GetMouseButton (1)) {
Debug.Log ("Right mouse button is held down");
}else if (Input.GetMouseButtonUp (1)) {
Debug.Log ("Right mouse button is released");
}
}
}