Here's an example of a C# MonoBehaviour class that takes in the mouse position and click input using the new Input System in Unity:
using UnityEngine;
using UnityEngine.InputSystem;
public class MouseInputHandler : MonoBehaviour
{
private Vector2 mousePosition;
private bool isLeftMouseButtonClicked;
private bool isRightMouseButtonClicked;
private void OnEnable()
{
// Enable the mouse input actions
Mouse.current.position.performed += OnMousePositionPerformed;
Mouse.current.leftButton.performed += OnLeftMouseButtonPerformed;
Mouse.current.rightButton.performed += OnRightMouseButtonPerformed;
}
private void OnDisable()
{
// Disable the mouse input actions
Mouse.current.position.performed -= OnMousePositionPerformed;
Mouse.current.leftButton.performed -= OnLeftMouseButtonPerformed;
Mouse.current.rightButton.performed -= OnRightMouseButtonPerformed;
}
private void OnMousePositionPerformed(InputAction.CallbackContext context)
{
// Update the mouse position
mousePosition = context.ReadValue<Vector2>();
}
private void OnLeftMouseButtonPerformed(InputAction.CallbackContext context)
{
// Update the left mouse button click state
isLeftMouseButtonClicked = context.ReadValueAsButton();
}
private void OnRightMouseButtonPerformed(InputAction.CallbackContext context)
{
// Update the right mouse button click state
isRightMouseButtonClicked = context.ReadValueAsButton();
}
private void Update()
{
// Use the mouse position and click states in your game logic
Debug.Log("Mouse Position: " + mousePosition);
Debug.Log("Left Mouse Button Clicked: " + isLeftMouseButtonClicked);
Debug.Log("Right Mouse Button Clicked: " + isRightMouseButtonClicked);
}
}
In this example, the MouseInputHandler class uses the new Input System to handle mouse input. Here's how it works:
-
The OnEnable method is called when the script is enabled. It subscribes to the performed events of the Mouse.current.position, Mouse.current.leftButton, and Mouse.current.rightButton input actions.
-
The OnDisable method is called when the script is disabled. It unsubscribes from the performed events to avoid memory leaks.
-
The OnMousePositionPerformed method is called whenever the mouse position changes. It reads the mouse position value from the input action callback context and stores it in the mousePosition variable.
-
The OnLeftMouseButtonPerformed method is called when the left mouse button is clicked or released. It reads the button state from the input action callback context and updates the isLeftMouseButtonClicked variable accordingly.
-
The OnRightMouseButtonPerformed method is similar to the left mouse button method but handles the right mouse button click state.
-
The Update method is called every frame. In this example, it simply logs the current mouse position and click states to the console. You can replace this with your own game logic that utilizes the mouse input.
Make sure to have the new Input System package installed in your Unity project and set up the appropriate input actions for the mouse in the Input Action Asset.
You can attach this script to a GameObject in your scene, and it will handle the mouse input using the new Input System.