InControl: Persisting Bindings

Action sets can be saved and loaded along with all their contained actions and bindings.

It’s worth mentioning that there are two kinds of bindings on actions. Those set up with PlayerAction.AddDefaultBinding are marked as preset system bindings. They can be removed from an action, but they are remembered so if the action is reset with either with PlayerActionSet.Reset or PlayerAction.Reset then they will be restored.

Bindings added either through listening or, less commonly, with the PlayerAction.AddBinding method are internally marked as custom bindings. If you remove them, they will not be restored if the action is reset. This provides an easy way to create sensible default bindings that can be restored easily to a either a single action or all actions.

Saving and loading is done, as you might imagine, with two simple methods on PlayerActionSet:

public string Save();
void Load( string data );

The data string contains the bindings for each action by name and, when loaded, are applied to actions in the set individually by name. If you rename or remove an action, loading will simply ignore the data for this old action. Actions have their bindings cleared before saved bindings are applied, but only if data is available to apply for it. If you add an action that does not have bindings in the data, it will simply be ignored and will keep the default bindings you created for it.

Beyond this, the actual storage of the data string is up to you. A simple way of handling them is just to put them in Unity’s PlayerPrefs:

void SaveBindings()
{
    saveData = characterActions.Save();
    PlayerPrefs.SetString( "Bindings", saveData );
}


void LoadBindings()
{
    if (PlayerPrefs.HasKey( "Bindings" ))
    {
        saveData = PlayerPrefs.GetString( "Bindings" );
        characterActions.Load( saveData );
    }
}

And that’s it! For more information, please see the API reference.