Skip to content

Releases: imDanoush/Unity-DirectInput

Direct Input Manager v1.0f

11 Jan 21:05
Compare
Choose a tag to compare

🎮 Unity DirectInput Force Feedback & Input Mapping System - Major Update

image

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
  • 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 with GetForegroundWindow 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

Demo Software

11 Jan 21:19
433ea03
Compare
Choose a tag to compare

Unity DirectInput Force Feedback Demo 🎮

Experience the power of DirectInput force feedback with this comprehensive demo application! This showcase demonstrates the capabilities of the Unity DirectInput system with an intuitive, runtime-configurable interface.

Key Features

  • Real-time FFB Configuration - Fine-tune your force feedback effects during gameplay, including:

    • Basic forces (Constant, Damper, Friction, Inertia, Spring)
    • Periodic effects (Sine, Square, Triangle, Sawtooth)
    • Collision simulation testing
  • Dynamic Input Mapping - Configure your DirectInput device mappings on the fly:

    • Visual input detection system
    • Real-time axis value monitoring
    • Automatic device detection
    • Persistent configuration storage

User Experience

  • Toggle FFB configuration with F3
  • Access input mapping with F2
  • Right-click sliders to reset values
  • Save/load configurations between sessions
  • Smart device search and auto-connection

Perfect for racing game developers and simulation enthusiasts looking to implement professional-grade force feedback and input systems in their Unity projects.

Built based on the DirectInput plugin of MrTimCackes and ATG-Simulator's improvements, this demo provides a complete reference implementation for DirectInput device management in Unity with Unity's Input System.