🎮 Unity DirectInput Force Feedback & Input Mapping System - Major Update
VERY IMPORTANT: This is the core plugin, after installing this core plugin into Unity, you may install this example scene project to discover the features and quick start working with it.
This comprehensive update improves DirectInput force feedback implementation, enhances stability, and adds new runtime configuration features with significant under-the-hood improvements.
Core Architecture Changes
- Replaced SAFEARRAY with direct memory management (const char**) for string arrays
- Added proper memory cleanup routines with RAII patterns
- Improved error handling with HRESULT returns
- Updated C# interop layer for reliable marshaling
- FFB device re-attach now works without DInput Mapper interruptions
- Fixed string marshaling for P/Invoke calls using CharSet.Ansi with [MarshalAs(UnmanagedType.LPStr)]
New Features
-
Runtime FFB Configuration UI (F3 key toggle)
- Complete force feedback effects control panel
- Live axis value monitoring
- Device auto-detection with search functionality
- Persistent settings via PlayerPrefs
-
Runtime Input Mapping UI (F2 key toggle)
- Real-time input assignment system
- Visual feedback for the Input mapping process
- Right-click to reset values
- Duplicate mapping name detection
Editor Enhancements
- Edit mode mapping configuration
- Device search term management
- Input/device string editing
- Add/remove mapping functionality
- Improved error handling and validation
Technical Improvements
-
Memory Management
- Proper GUI cleanup and initialization
- Enhanced error handling for device connections
- Streamlined save/load system
- Eliminated SafeArray versioning issues
- Added proper cleanup of unmanaged resources
- Fixed memory leaks in effect creation and device management
- Ensured all dynamically allocated memory is properly freed
-
Error Handling and Stability
- Added comprehensive try/catch blocks around all exported functions
- Implemented detailed logging throughout the codebase
- Created a robust error message system with
GetLastError
function - Added null checks and validation for all input parameters
- Better runtime vs edit mode separation
- Enhanced error reporting with detailed messages
-
Unity Integration
- Added Unity plugin lifecycle functions (
UnityPluginLoad
/UnityPluginUnload
) - Implemented version tracking via
GetPluginVersion()
for compatibility checks - Added initialization state tracking to prevent premature operations
- Added Unity plugin lifecycle functions (
-
Device Communication
- Added DBTEvents enum for device state changes (0x0007, 0x8000, 0x8004)
- Implemented proper callback mechanism with __stdcall convention
- Added static callback storage (g_deviceCallback) to prevent garbage collection
- Fixed callback parameter type matching between C# and C++
- Replaced
FindMainWindow
withGetForegroundWindow
for better compatibility
Code Quality Improvements
- Added proper initialization for static collections like _activeDevices dictionary
- Added [In] attribute for array parameters in UpdateFFBEffect method
- Changed P/Invoke method visibility from public to internal following best practices
- Fixed Debouncer implementation with proper field initialization
- Addressed NullReferenceException by initializing dictionary fields
- Removed unnecessary object initialization before out parameters
- Modified UpdateEffect method to modify existing DICondition objects rather than creating new ones
- Fixed MD5CryptoServiceProvider usage with MD5.Create() pattern
- Added readonly modifier to immutable fields
Bug Fixes
- Fixed marshaling of string parameters to correctly handle ANSI strings in native code
- Corrected memory management for P/Invoke calls
- Prevented potential NullReferenceExceptions in device management
- Fixed initialization issues in force feedback effect handling
- Fixed device state handling to prevent data loss
- Enhanced debugging capabilities via the
DEBUG1
function - Fixed issues with DPAD bit shifting in the
FlattenDIJOYSTATE2
function
Files Modified
- DirectInputForceFeedback.h
- DirectInputForceFeedback.cpp
- DirectInputManager.cs
- DirectInputTypes.cs
- [UI related files]
Testing & Validation
- Verified string array marshaling works across different machines
- Confirmed memory cleanup works correctly
- Validated error handling in edge cases
- Tested UI functionality in both runtime and edit modes
- Ensured backward compatibility with existing applications
- Verified cross-platform compatibility in both Unity and standalone applications