Course contentsShow
← Unity Game Development
Lesson 64 of 8281. Unity FoundationsFree lesson

Building Custom Input Rebinding Menus

Learners will implement a dynamic rebinding system allowing players to remap their own control keys during gameplay for improved accessibility.

In this lesson, you will learn how to create a menu that allows players to redefine their own control keys, making your game accessible to everyone’s preferred playstyle.

Beyond Hardcoded Controls

In previous lessons, we used the New Input System to map actions (like "Jump") to specific keys (like the Space bar). However, if a player wants to use the "E" key to jump instead, hardcoded inputs prevent that choice. A custom rebinding menu solves this by allowing the game to overwrite these internal mappings at runtime.

How Rebinding Works

At the heart of the system is the InputActionRebindingExtensions class. When you trigger a rebind, the system stops listening for the old key and enters a "listening state," waiting for the user to press a new button on their keyboard or controller. Once detected, it updates the InputAction map automatically.

Here is a simplified look at how you start the rebinding process in a script:

public void StartRebinding(InputAction actionToRebind)
{
    // Disable the action map so we don't trigger game logic during rebinding
    actionToRebind.Disable();

    // Perform the rebind operation
    actionToRebind.PerformInteractiveRebinding()
        .OnComplete(operation => 
        {
            // Clean up the memory and re-enable the action
            operation.Dispose();
            actionToRebind.Enable();
        })
        .Start();
}

Essential Steps for Implementation

  1. Prepare the UI: Create a button for each action you want the player to customize.
  2. Handle States: Always call Disable() on your InputAction before rebinding. If you don't, the game might interpret the "new key" press as a command to jump or shoot while you are trying to configure the settings!
  3. Save and Load: Once a rebind is successful, the Input System generates a "binding override." You must save this string data (usually using PlayerPrefs or a JSON file) so that when the player closes and reopens the game, their custom keys remain intact.
  4. Cleanup: Always call Dispose() on your rebinding operation after completion to prevent memory leaks.

Key Takeaway: By using PerformInteractiveRebinding, you delegate the complex work of listening for device input to Unity, allowing you to focus on saving those changes so the player's preferences persist across game sessions.