This guide will help you set up robust runtime key rebindings in Unity using the Input System package, including support for saving and loading user preferences.
- Unity Input System: Install the Input System package via Package Manager.
- Sample Assets: Download the Input System samples for rebinding (available in the package samples).
This ReadMe was made by AI the code isn't I am just too lazy to write this out.
-
Import Samples
- In Package Manager, select Input System > Samples > "Rebinding UI" (or similar) > Import.
- Locate the sample assets in your project, e.g.,
KeyboardRebindingUIandGamepadRebindingUI.
-
Add Rebind UI to Scene
- Drag the
KeyboardRebindingUIprefab into your scene. - (Optional) Drag in
GamepadRebindingUIif you want gamepad support.
- Drag the
-
Configure RebindActionUI
- Assign the desired action references in the
RebindActionUIcomponent for each rebindable input. - Make sure the actions match those configured in your
PlayerInputcomponent.
- Assign the desired action references in the
-
Saving and Loading Rebinds
- In any UI/menu handler script that manages menus or input settings, call:
InputAction.SaveBindingOverridesAsJson()when disabling/closing the UI (e.g., inOnDisable).InputAction.LoadBindingOverridesFromJson()when opening/loading the UI (e.g., inOnEnable).
- Store the JSON string in
PlayerPrefsor any other persistent storage.
- In any UI/menu handler script that manages menus or input settings, call:
-
Consistency Across Scenes
- Ensure the same
PlayerInputasset and action map are used across all relevant scenes and UI objects. - Avoid duplicating input definitions between scenes to maintain user rebinds.
- Ensure the same
- Make sure all objects referencing
PlayerInputuse the same asset to ensure bindings persist. - Rebinding UI samples can be customized for different control schemes (keyboard/gamepad).
- For multiplayer setups, manage bindings per player or per input device as needed.
- If rebinds do not persist, verify that you are saving/loading binding overrides correctly and using the same action asset everywhere.
- Always test key rebinds and their persistence after a scene reload or application restart.