Search Unity

How to save Input Action bindings

Discussion in 'Input System' started by ChrisJohnson, Dec 25, 2019.

  1. ChrisJohnson

    ChrisJohnson

    Joined:
    Feb 20, 2013
    Posts:
    64
    Hello, I'm currently working on rebind-able controls for a game that I'm working on. I got a menu for rebinding the games input actions working. But every time I change a scene or restart the game all of the bindings disappear.

    I was wondering, what is the recommended way of saving, and loading the input bindings.

    Here is the code that I have for rebinding a control:

    Code (CSharp):
    1. public class InputActionRebinder : MonoBehaviour
    2. {
    3.     public InputAction inputAction;
    4.     public TMP_Text text;
    5.     public int bindingIndex;
    6.  
    7.     void Start()
    8.     {
    9.         SetActionText();
    10.     }
    11.  
    12.     public void Rebind(Button button)
    13.     {
    14.         button.enabled = false;
    15.         text.text = "Press any key";
    16.  
    17.         inputAction.PerformInteractiveRebinding()
    18.         .WithTargetBinding(bindingIndex)
    19.         .WithControlsExcluding("Mouse")
    20.         .OnMatchWaitForAnother(0.1f)
    21.         .OnComplete(operation => OnRebind(operation, button))
    22.         .Start();
    23.     }
    24.  
    25.     private void SetActionText()
    26.     {
    27.         text.text = InputControlPath.ToHumanReadableString(inputAction.bindings[bindingIndex].effectivePath, InputControlPath.HumanReadableStringOptions.OmitDevice);
    28.     }
    29.  
    30.     private void OnRebind(InputActionRebindingExtensions.RebindingOperation operation, Button button)
    31.     {
    32.         SetActionText();
    33.         operation.Dispose();
    34.         button.enabled = true;
    35.     }
    36. }
     
    ZachariBarnes likes this.
  2. ZachariBarnes

    ZachariBarnes

    Joined:
    Nov 6, 2017
    Posts:
    12
    Bump, Would love to see a response to this issue as I am having the same problem.
     
  3. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,422
    Yeah, gonna bump this as well. I have a "settings" file where different non-gamedata stuff is saved (Audio Settings, Accessibility settings etc.), there I would save the Mapping, but the question is:
    what exactly needs to be serialized/saved and deserialized/loaded?
     
  4. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Basically, only InputBinding.overridePath needs to be stored and correlated by InputBinding.id. I've posted a sample code snippet here.

    An API for getting a JSON string with overrides in a single call and for restoring all overrides in a single call will go in right after 1.0.

    For scene transitions, I would recommend keeping the input setup intact and across rather than re-creating it on every scene load. For example, when using PlayerInput, by making it DontDestroyOnLoad and when using generated C# wrappers, by keeping the instance around.
     
    patrickgh3, ZachariBarnes and BTStone like this.
  5. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,091
    Was added in 1.1 preview, from changelog:
    Code (CSharp):
    1. void SaveUserRebinds(PlayerInput player)
    2. {
    3.     var rebinds = player.actions.SaveBindingOverridesAsJson();
    4.     PlayerPrefs.SetString("rebinds", rebinds);
    5. }
    6.  
    7. void LoadUserRebinds(PlayerInput player)
    8. {
    9.     var rebinds = PlayerPrefs.GetString("rebinds");
    10.     player.actions.LoadBindingOverridesFromJson(rebinds);
    11. }
    12.  
     
  6. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    Code (CSharp):
    1. actions.SaveBindingOverridesAsJson();
    Any idea as to why this would give me an empty string?
     
  7. talhakaya

    talhakaya

    Joined:
    Jun 7, 2013
    Posts:
    6
    I have the same problem, SaveBindingOverridesAsJson() returns an empty string. Input System 1.3.0
     
    udemyrootgames likes this.
  8. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Need more details than that. Most common source would be that the set of actions being queried are not the ones actually holding the overrides. E.g. when PlayerInput duplicates the set of actions. Or when "Generate C# Class" is incorrectly mixed with PlayerInput.