Skip to main content

Input Binding Persistence Generator

Plan how to save, load, and reset rebinding overrides.

Your choices shape storage keys, reset behavior, and a minimal C# pattern using SaveBindingOverridesAsJson / LoadBindingOverridesFromJson. Pair with conflict checker before shipping presets.

// Starter pattern (Input System) — adapt namespaces
using UnityEngine;
using UnityEngine.InputSystem;

public static class BindingPersistence
{
    const string Key = "bindings_v1_overrides";

    public static void SaveOverrides(InputActionAsset asset)
    {
        var json = asset.SaveBindingOverridesAsJson();
        System.IO.File.WriteAllText(System.IO.Path.Combine(Application.persistentDataPath, "bindings.json"), json);
    }

    public static void LoadOverrides(InputActionAsset asset)
    {
        var path = System.IO.Path.Combine(Application.persistentDataPath, "bindings.json"); var json = System.IO.File.Exists(path) ? System.IO.File.ReadAllText(path) : "";
        if (!string.IsNullOrEmpty(json))
            asset.LoadBindingOverridesFromJson(json);
    }

    public static void ResetToDefaults(InputActionAsset asset)
    {
        asset.LoadBindingOverridesFromJson(null);
        var path = System.IO.Path.Combine(Application.persistentDataPath, "bindings.json"); if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
    }
}
Will this compile in my project?
Snippets are templates — adjust namespaces, asset references, and error handling for your game.
Why avoid binding indices?
Reordering bindings in the editor changes indices; stable IDs or paths survive asset edits better.