A Unity plugin that bridges native iOS input events (mouse, keyboard) to Unity's event system. This enables proper handling of hardware peripherals connected to iOS devices.
- Mouse Support: Capture mouse movements, button clicks, and scroll wheel events
- Keyboard Support: Capture key press and release events with key codes
- Event-Based Architecture: Clean C# event system for easy integration
- Sample Scene: Included demo scene showing how to use all features
- Easy Integration: Simple API with minimal setup required
- Extensible: Built on the gc-input-events Objective-C library
- Unity 2022.3 LTS or later
- iOS deployment target
- Xcode (for iOS builds)
- Open your Unity project
- Go to Window > Package Manager
- Click the + button and select Add package from git URL
- Enter:
https://github.com/manaporkun/unity-ios-input-plugin.git
- Download or clone this repository
- Copy the
Assets/PluginsandAssets/Scriptsfolders into your Unity project'sAssetsfolder
Call RegisterEvents() on application start to initialize the native input system:
using UnityEngine;
public class InputManager : MonoBehaviour
{
private void Start()
{
// Only initialize on iOS devices
if (Application.platform != RuntimePlatform.IPhonePlayer) return;
// Initialize mouse events
GCMouseBridge.RegisterEvents();
// Initialize keyboard events
GCKeyboardBridge.RegisterEvents();
}
}private void OnEnable()
{
#if UNITY_IOS
GCMouseBridge.OnMouseMoved += HandleMouseMoved;
GCMouseBridge.OnMouseButtonPressed += HandleMouseButton;
GCMouseBridge.OnMouseScrolled += HandleMouseScroll;
#endif
}
private void OnDisable()
{
#if UNITY_IOS
GCMouseBridge.OnMouseMoved -= HandleMouseMoved;
GCMouseBridge.OnMouseButtonPressed -= HandleMouseButton;
GCMouseBridge.OnMouseScrolled -= HandleMouseScroll;
#endif
}
private void HandleMouseMoved(float deltaX, float deltaY)
{
Debug.Log($"Mouse moved: ({deltaX}, {deltaY})");
}
private void HandleMouseButton(int buttonId, bool pressed)
{
// buttonId: 0 = left, 1 = right, 2 = middle
string state = pressed ? "pressed" : "released";
Debug.Log($"Mouse button {buttonId} {state}");
}
private void HandleMouseScroll(float scrollX, float scrollY)
{
Debug.Log($"Mouse scrolled: ({scrollX}, {scrollY})");
}private void OnEnable()
{
#if UNITY_IOS
GCKeyboardBridge.OnKeyPressed += HandleKeyPressed;
#endif
}
private void OnDisable()
{
#if UNITY_IOS
GCKeyboardBridge.OnKeyPressed -= HandleKeyPressed;
#endif
}
private void HandleKeyPressed(long keyCode, bool pressed)
{
string state = pressed ? "pressed" : "released";
Debug.Log($"Key {keyCode} {state}");
}| Member | Type | Description |
|---|---|---|
RegisterEvents() |
Method | Initializes the native mouse event system. Call once at startup. |
OnMouseMoved |
Event | Fired when the mouse moves. Parameters: (float deltaX, float deltaY) |
OnMouseButtonPressed |
Event | Fired when a mouse button is pressed/released. Parameters: (int buttonId, bool pressed) |
OnMouseScrolled |
Event | Fired when the scroll wheel is used. Parameters: (float scrollX, float scrollY) |
| Member | Type | Description |
|---|---|---|
RegisterEvents() |
Method | Initializes the native keyboard event system. Call once at startup. |
OnKeyPressed |
Event | Fired when a key is pressed/released. Parameters: (long keyCode, bool pressed) |
The repository includes a sample scene (Assets/Scenes/SampleScene.unity) that demonstrates:
- How to initialize the input bridges
- How to subscribe to all available events
- How to display input information on screen using TextMesh Pro
To run the sample:
- Open
SampleScenein Unity - Build and deploy to an iOS device
- Connect a mouse or keyboard to see the input events
┌─────────────────────────────────────────────────────────────┐
│ Your Unity Game │
├─────────────────────────────────────────────────────────────┤
│ GCMouseBridge.cs │ GCKeyboardBridge.cs │
│ - OnMouseMoved │ - OnKeyPressed │
│ - OnMouseButtonPressed │ │
│ - OnMouseScrolled │ │
├─────────────────────────────────────────────────────────────┤
│ libGCInputEvents.a (Native iOS) │
│ P/Invoke Interop Layer │
├─────────────────────────────────────────────────────────────┤
│ iOS Game Controller Framework │
└─────────────────────────────────────────────────────────────┘
- gc-input-events - The Objective-C/Xcode project containing the native iOS library source code
- Ensure you called
RegisterEvents()before subscribing to events - Verify you're running on an actual iOS device (not the simulator)
- Check that a mouse/keyboard is properly connected
The bridge classes use #if UNITY_IOS preprocessor directives. If you're seeing compilation errors:
- Ensure the assembly definition (
iOSInputPlugin.asmdef) is configured for iOS and Editor platforms only - Wrap your event subscriptions in
#if UNITY_IOSblocks
If you get "DllNotFoundException" errors:
- Verify
libGCInputEvents.aexists inAssets/Plugins/iOS/ - Check that the library is included in your Xcode build settings
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
See CHANGELOG.md for a list of changes.
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
- Built using Apple's Game Controller framework
- Uses Unity's P/Invoke for native interop