Hello everyone.
I am upgrading from my custom input manager to Unity’s new input manager and I’d like to know if it is possible to change keybindings at runtime with it?
If yes, any ideas how?
Hello everyone.
I am upgrading from my custom input manager to Unity’s new input manager and I’d like to know if it is possible to change keybindings at runtime with it?
If yes, any ideas how?
myAction.ApplyBindingOverride(newBindingPath);
For interactive rebinding, use PerformInteractiveRebinding.
First of all thank you for the reply.
Just to make sure if I got that right, the “ApplyBindingOverride()” changes keybindings on start and “PerformInteractiveRebinding()” changes keybindings on runtime?
I’ve checked the documentation but I’m finding it to be very confusing.
As the documentation says, the PerformInteractiveRebinding will wait for an input from the user and binds it. Just like you see in other games.
When you call the ApplyBindingOverride method, you need to provide the new binding path in parameter.
Oh okay, so one returns a path and the other one takes that path as its parameter and applies it?
Nope.
myAction.ApplyBindingOverride(newBindingPath);
You as the developer give a new binding. For example previously you saved some user choosing to disk. You read it back from the disk and set the binding.
var rebindOperation = actionToRebind.PerformInteractiveRebinding()
// To avoid accidental input from mouse motion
.WithControlsExcluding("Mouse")
.OnMatchWaitForAnother(0.1f)
.Start();
This (or similar) will be waiting for the user during the game to give an input to rebind the action. Which you later can store on disk or in game save or whatever.
So one will rebind without user action, the other will rebind whatever the player chooses.
I get it now. Thank you!
I hope someone reads this message, but I have a problem with changing the bindings and I don’t know what’s the reason… This is my code:
InputBinding newPath = new InputBinding("<Keyboard>/enter");
_input.GamePlay.Fire.ApplyBindingOverride(newPath);
But this doesn’t work. The path stays the same, whatever I try… Is there someone who can shed some light on this issue ? Thanks !
I second that this is super confusing, because there is both a ChangeBinding and a ApplyBindingOverride.
Neither do anything, im calling this on a generated class’s action, and the old binding still responds, the new one is ignored.
I have to say the input system API is very counter intuitive… @Rene-Damm what are we doing wrong?
I’ve got it to work on Unity 2020.2.1f1 by the following code. The trick may be to specify the original path/group, like this:
inputAction.ApplyBindingOverride(new InputBinding
{
// Explicitly target the original one by its path - this code assumes that Control Scheme is present
// and is named "Keyboard," whose only binding is a keyboard key.
// I believe you could also specify `groups` instead (Control Scheme required.)
path = inputAction.bindings.First(b => b.groups == "Keyboard").path,
overridePath = overridePath, // e.g. <keyboard>/enter
});
Apparently, it is expected at this point that inputAction.path
stays the same.
Rather, check inputAction.overridePath
and inputAction.effectivePath
. If those points to what you have specified, then it should work.
I wonder if one can just do $"<Keyboard>/{InputSystem.Key.EnumValue}"
to specify the overridePath
, though. I hope that’s the case.
I reported this as an issue, and got the following response: