An open-source Android library for Unity that enables pointer capture functionality on Android devices, allowing games to capture and control mouse/pointer input.
This project provides a way to implement pointer capture in Unity games running on Android. By default, Unity's behavior with mouse input is to stop once the mouse has reached the edge of the screen. This plugin allows you to continue moving with your mouse indefinitely, which is essential for first-person mouse gameplay. When enabled, it captures all mouse movements and button presses, preventing the system cursor from appearing and allowing your game to have full control over pointer input. This is especially useful for first-person games, 3D navigation, or any application requiring continuous mouse input without cursor interference.
- Pointer Capture: Capture mouse/pointer movements on Android devices (API 26+)
- Unity Integration: Easy-to-use Unity package with simple API
- Automatic State Management: Ties capture state to Unity's
Cursor.lockState - Input Compatibility: Drop-in replacement for Unity's Input system methods
- Button Support: Handles up to 7 mouse buttons (left, right, middle, and 4 additional buttons)
- Scroll Wheel: Captures both vertical and horizontal scroll input
- Activity Lifecycle Aware: Automatically handles Android activity lifecycle events
- Unity 2021.3 or later
- Android API 26 (Android 8.0 Oreo) or higher
- Android Studio (for building the AAR library)
├── AndroidInputCapture/ # Android library source
│ ├── app/ # Main library module
│ │ └── src/main/java/com/example/androidinputcapture/
│ │ └── PointerCaptureHelper.java
│ └── unityLibrary/ # Unity library dependencies
│
└── AndroidInputCaptureUnityDemo/ # Unity demo project
└── Assets/OpenPointerCapture/
├── Plugins/ # Contains android-input-capture.aar
├── Scripts/ # Unity C# scripts
│ ├── PointerCaptureManager.cs
│ ├── PointerCaptureNativeInterface.cs
│ └── CapturedInput.cs
└── Demo/ # Demo scene and scripts
- Copy the
AndroidInputCaptureUnityDemo/Assets/OpenPointerCapturefolder to your Unity project's Assets folder - The package includes a pre-built AAR file ready to use
- Open the
AndroidInputCaptureproject in Android Studio - Build the project to generate the AAR file
- Copy the generated AAR to your Unity project's
Assets/Plugins/Androidfolder - Copy the Unity scripts from
AndroidInputCaptureUnityDemo/Assets/OpenPointerCapture/Scriptsto your project
- Add
PointerCaptureManagercomponent to a GameObject in your scene - The manager automatically captures pointer input when
Cursor.lockStateis set toCursorLockMode.Locked
Replace Unity's Input methods with CapturedInput equivalents:
using OpenPointerCapture;
// Instead of Input.GetMouseButtonDown(0)
if (CapturedInput.GetMouseButtonDown(0))
{
// Handle left click
}
// Instead of Input.GetAxis("Mouse X")
float mouseX = CapturedInput.GetAxis("Mouse X");
// Instead of Input.mouseScrollDelta
Vector2 scrollDelta = CapturedInput.mouseScrollDelta;
// Instead of Input.mousePosition
Vector3 mousePos = CapturedInput.mousePosition;If you need manual control over pointer capture:
// Begin capture
PointerCaptureNativeInterface.beginCapture();
// End capture
PointerCaptureNativeInterface.endCapture();
// Check capture state
bool isCaptured = PointerCaptureNativeInterface.isPointerCaptured();Subscribe to pointer events directly:
void OnEnable()
{
PointerCaptureManager.OnCapturedPointerMoved += HandlePointerMove;
PointerCaptureManager.OnCapturedMouseButton += HandleMouseButton;
PointerCaptureManager.OnCapturedScroll += HandleScroll;
}
void HandlePointerMove(Vector2 delta)
{
// Handle mouse movement
}
void HandleMouseButton(int buttonIndex, bool isDown)
{
// Handle button press/release
}
void HandleScroll(Vector2 scrollDelta)
{
// Handle scroll wheel
}The project includes a comprehensive demo scene showcasing various features:
- Camera rotation with captured mouse movement
- Object interaction with mouse buttons
- UI elements that respond to captured input
- Visual feedback for button presses and scroll events
To run the demo:
- Open
AndroidInputCaptureUnityDemoin Unity - Load the scene
Assets/OpenPointerCapture/Demo/OpenPointerCaptureDemo.unity - Build and run on an Android device
The PointerCaptureHelper class:
- Implements Android's
OnCapturedPointerListenerinterface - Manages activity lifecycle callbacks
- Stores input deltas and button states for Unity to poll
- Handles view attachment/detachment automatically
- PointerCaptureNativeInterface: JNI bridge to communicate with Android
- PointerCaptureManager: MonoBehaviour that polls for input and fires events
- CapturedInput: Static facade providing Input-compatible API
- Requires Android API 26+ (Android 8.0 Oreo)
- Only works on Android devices with mouse/pointer support
- In Unity Editor, falls back to standard Input methods
- Ensure your Android device is running API 26 or higher
- Check that the AndroidManifest.xml includes the library
- Verify that
PointerCaptureManageris active in the scene
- The system polls for input each frame, ensure stable frame rates
- For high-frequency input, consider increasing the polling rate
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.