- Home /
How to bind multiple actions at once with unity's new input system?
I'm doing a game that has multiple action maps, some of the inputs are the same between these action maps like sprinting. I understand how to bind sprint on one action map but how do I bind it on the other without asking the player to press the button again?
using UnityEngine.InputSystem;
InputActionRebindingExtensions.RebindingOperation rebindingOp;
void StartRebind ()
{
rebindingOp = actionMapBinding.action.PerformInteractiveRebinding()
.WithControlsExcluding("Mouse")
.WithCancelingThrough("Touchpad")
.WithCancelingThrough("Escape")
.WithCancelingThrough("Select")
.WithCancelingThrough("Start")
.OnMatchWaitForAnother(.1f)
.OnComplete(operation => RebindComplete())
.Start();
}
void RebindComplete ()
{
int bindingIndex = actionMapBinding.action.GetBindingIndexForControl(actionMapBinding.action.controls[0]);
if (HasDuplicateBindings(actionMapBinding.action, bindingIndex, false))
{
Debug.Log("Binding already taken");
actionMapBinding.action.RemoveBindingOverride(bindingIndex);
rebindingOp.Dispose();
StartRebind();
return;
}
inputs[selection].GetComponentInChildren<TMP_Text>().text = InputControlPath.ToHumanReadableString(
actionMapBindings.action.bindings[bindingIndex].effectivePath ,
InputControlPath.HumanReadableStringOptions.OmitDevice
);
}
First, let me say that I've not tried this, but it seems like a good UX idea for some cases. I'd normally use only one map for all those cases, set a different binding group for different controls that are usable in different cases, and deactivate/activate control groups with a binding mask according to the current use case. Or I'd just make users bind actions twice if they seem different enough. But your idea seems cleaner for a lot of uses. Have you tried using ApplyBindingOverride to copy the binding from the first action to a second one on RebindComplete?
Answer by kikchitov · Apr 01 at 09:31 AM
using UnityEngine.InputSystem;
void OnRebindingComplete (
InputActionRebindingExtensions.RebindingOperation operation ,
int bindingIndex ,
params InputAction[] duplicates
)
{
foreach( var duplicate in duplicates )
duplicate.ApplyBindingOverride( bindingIndex , operation.selectedControl.path );
}
Your answer
Follow this Question
Related Questions
New Input System 'Started' and 'Performed' actions fire at the same time? 1 Answer
SteamVR Input window not loading properly 0 Answers
How to consume input when automatically switching control schemes through a Player Input Component 0 Answers
How can I detect when a binding is pressed in the new input system? 0 Answers
XR controller - New Input System - .perform x .canceled - small float values x dead zone 0 Answers