What’s the correct way to do this ? I searched through the doc and github to no avail
Thanks !
EDIT : Also, I’m on AZERTY layout but my Z key is displayed as W (because of the location mapping). Is there a way to get the “real” name of the control ?
Instead of using :
InputControlPath.ToHumanReadableString(movementReference.action.bindings[2].effectivePath, InputControlPath.HumanReadableStringOptions.OmitDevice)
I’m using :
movementReference.action.controls[0].displayName
Still don’t know how I’m supposed to get the indexes (hardcode it ?) or how to rebind at runtime though
We’re in the process of putting the finishing touches on an extensive pass over binding display strings and rebinding UI support including a sample for how to put together a rebind UI using the APIs (and also including a ready-made prefab and scripting component that you can use in your own projects). This includes support for dealing with composites both in case you want to rebind them as a whole as well as when splitting them up into individual rebinds. The changes should be in the next package.
And the “onComplete” never triggers and I can’t remap. Tried without the BindingGroup, without the BindingTarget, without the ExcludeMouse, Without the Cancel, tried all combinations and I keep failing. What am I missing?
So the trick is that a composite is a binding, but it’s also made of bindings. So for this action :
action.bindings[0] is WASD, and is a 2D vector. PerformInteractiveRebinding will only accept a 2D vector like a left stick.
However action.bindings[1] is the W key ! If you do PerformInteractiveRebinding with WithTargetBinding(1) you can rebind it. The only problem is that it will still only accept a 2D vector, so you need to use useWithExpectedControlType(“Button”).
Here’s my script (use it on a button) you’ll need to set the bindingIndex int manually in editor depending on your setup.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;
public class RebindButton : MonoBehaviour
{
public InputActionReference actionReference;
public int inputBindingIndex;
public TextMeshProUGUI buttonText;
private InputActionRebindingExtensions.RebindingOperation rebindingOperation;
private Button button;
private void OnValidate()
{
inputBindingIndex = Mathf.Clamp(inputBindingIndex, 0, actionReference.action.bindings.Count-1);
}
void Start()
{
button = GetComponentInChildren<Button>();
button.onClick.AddListener(StartRebind);
buttonText.text = InputControlPath.ToHumanReadableString(actionReference.action.bindings[inputBindingIndex].effectivePath);
}
private void OnDestroy()
{
button.onClick.RemoveAllListeners();
}
public void StartRebind()
{
actionReference.action.Disable();
print("launching rebind");
var rebindingOperation = actionReference.action.PerformInteractiveRebinding()
.WithControlsExcluding("Mouse/delta")
.WithControlsExcluding("Mouse/position")
.WithCancelingThrough("<Keyboard>/escape")
.WithTargetBinding(inputBindingIndex)
.Start()
.OnComplete((x) =>
{
// update button string
buttonText.text = InputControlPath.ToHumanReadableString(x.action.bindings[inputBindingIndex].effectivePath);
actionReference.action.Enable();
x.Dispose();
})
.OnCancel((x)=>
{
// deselect button
FindObjectOfType<EventSystem>().SetSelectedGameObject(null);
x.Dispose();
actionReference.action.Enable();
});
if (actionReference.action.bindings[inputBindingIndex].isPartOfComposite)
{
rebindingOperation.WithExpectedControlType("Button");
}
}
}
Sorry, wanted to have all this out before Christmas but things came together slower than expected and I really needed a break…
The PR with the rebinding changes and a new rebinding UI sample has landed and is on the develop branch. 1.0.0-preview.4 is slated for early next week.
I’ve recorded a video with a quick fly-through of the changes.
If you get a chance to give the new stuff a try, let me know it goes
Think it should be fine. Most of the changes have been mere extensions and shouldn’t be break. PerformInteractiveRebinding() did change a bit under the hood to perform more configuration out of the box but if that should get in the way, you can just reset things to get back to how it was before.
Some feedback : I like that the display strings are localized using system language, but I haven’t found a way to manually set it. I’m a french dev making games in english and seeing “press ESPACE to jump” is a bit jarring.