Can i modify the Input Manager via script?

Hi everyone, i have a question about the Input Manager, i would to know if i can modify the Input Manager by scripting. I will make a configuration section for my game, and i want to do to the player, the possibility to modify the keys that is used in the game. So, since I use Input Manager to manage the keys, i need to modify that keys in the Input Manager, not in my scripts. Then, i can do this? How?
(Sorry for bad english, is google translate :smile:)

1 Like

You can’t but its pretty easy to make your InputManger

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public static class GameInputManager
{
    static Dictionary<string, KeyCode> keyMapping;
    static string[] keyMaps = new string[6]
    {
        "Attack",
        "Block",
        "Forward",
        "Backward",
        "Left",
        "Right"
    };
    static KeyCode[] defaults = new KeyCode[6]
    {
        KeyCode.Q,
        KeyCode.E,
        KeyCode.W,
        KeyCode.S,
        KeyCode.A,
        KeyCode.D
    };

    static GameInputManager()
    {
        InitializeDictionary();
    }

    private static void InitializeDictionary()
    {
        keyMapping = new Dictionary<string, KeyCode>();
        for(int i=0;i<keyMaps.Length;++i)
        {
            keyMapping.Add(keyMaps[i], defaults[i]);
        }
    }

    public static void SetKeyMap(string keyMap,KeyCode key)
    {
        if (!keyMapping.ContainsKey(keyMap))
            throw new ArgumentException("Invalid KeyMap in SetKeyMap: " + keyMap);
        keyMapping[keyMap] = key;
    }

    public static bool GetKeyDown(string keyMap)
    {
        return Input.GetKeyDown(keyMapping[keyMap]);
    }
}

Then use it like this:

void Update()
{
      if (GameInputManager.GetKeyDown("attack"))
      {
            // attack code
      }
}

This will not only let you customize the keys but you can create whatever names you want, not just the few default ones that Unity provides. I only showed code to Initialize and set up new keymappings. As well as one GetKeyDown function. You’d want to extend this out all the normal functions (GetKey, GetKeyUp, etc)

6 Likes

This is helpful for keyboard input. Any chance you could add support for joystick (Xbox controller) input?

No. Unity keeps promising us a configurable input manager. But so far nothing.

Common practice is to fill the gap with something like rewired from the asset store.

Get a Custom Input Manager, it’s free and already supports controllers GitHub - daemon3000/InputManager: Custom InputManager for Unity

3 Likes