Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question New Input System rebinding issue

Discussion in 'Input System' started by Zolaire, Jan 7, 2021.

  1. Zolaire

    Zolaire

    Joined:
    May 23, 2020
    Posts:
    21
    The issue is I can currently open up the controls, set my new binding to a new button, use that button once and then it removes all bindings from that action. Thats what I think is going on atleast.
    This is my code for doing so:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.InputSystem;
    4. using TMPro;
    5. public class Keybindings : MonoBehaviour
    6. {
    7.     [SerializeField] private InputActionReference actionReference = null;
    8.     [SerializeField] private TextMeshProUGUI displayText = null;
    9.     [SerializeField] private PlayerInput playerInput;
    10.     private InputActionRebindingExtensions.RebindingOperation rebindingOperation;
    11.     [SerializeField] private int alternateBinding;
    12.     private void Awake()
    13.     {
    14.         displayText.text = DisplayText(alternateBinding);
    15.     }
    16.     public void StartRebinding()
    17.     {
    18.         if (playerInput) playerInput.SwitchCurrentActionMap("Input");
    19.         rebindingOperation = actionReference.action.PerformInteractiveRebinding()
    20.             .WithCancelingThrough("<Keyboard>/escape")
    21.             .OnMatchWaitForAnother(0.1f)
    22.             .OnComplete(operation => RebindComplete())
    23.             .Start();
    24.     }
    25.     private void RebindComplete()
    26.     {
    27.         displayText.text = DisplayText(alternateBinding);
    28.         if (playerInput) playerInput.SwitchCurrentActionMap("Player");
    29.         rebindingOperation.Dispose();
    30.     }
    31.     private string DisplayText(int alt) => InputControlPath.ToHumanReadableString(
    32.         actionReference.action.bindings[GetIndex(alt)].effectivePath,
    33.         InputControlPath.HumanReadableStringOptions.OmitDevice);
    34.     private int GetIndex(int alt) => actionReference.action.GetBindingIndexForControl(actionReference.action.controls[alt]);
    35. }
    36.