celinedrules

Unity Input System Rebinding

Apr 24th, 2021
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.20 KB | None | 0 0
  1. using JetBrains.Annotations;
  2. using UnityEngine;
  3. using UnityEngine.InputSystem;
  4.  
  5. public class KeybindManager : MonoBehaviour
  6. {
  7.     private InputActionRebindingExtensions.RebindingOperation rebindingOperation;
  8.     private InputActionSetupExtensions.BindingSyntax action;
  9.     private InputActionSetupExtensions.BindingSyntax binding;
  10.    
  11.    
  12.     [SerializeField] private InputActionAsset inputActions;
  13.     private InputActionMap gameplayActionMap;
  14.    
  15.     private InputAction action1;
  16.     private InputAction action2;
  17.     private InputAction action3;
  18.  
  19.    
  20.     private static KeybindManager instance;
  21.    
  22.     public static KeybindManager Instance
  23.     {
  24.         get
  25.         {
  26.             if (instance == null)
  27.                 instance = FindObjectOfType<KeybindManager>();
  28.  
  29.             return instance;
  30.         }
  31.     }
  32.  
  33.     private void Awake()
  34.     {
  35.         gameplayActionMap = inputActions.FindActionMap("Default");
  36.        
  37.         action1 = gameplayActionMap.FindAction("Action 1");
  38.         action2 = gameplayActionMap.FindAction("Action 2");
  39.         action3 = gameplayActionMap.FindAction("Action 3");
  40.     }
  41.  
  42.     private void Start()
  43.     {
  44.         var bindings = gameplayActionMap.actions[0].bindings;
  45.  
  46.         for (int i = 1; i < bindings.Count; i++)
  47.             UiManager.Instance.UpdateKeyText(char.ToUpper(bindings[i].name[0]) + bindings[i].name.Substring(1) , bindings[i].ToDisplayString());
  48.        
  49.         UiManager.Instance.UpdateKeyText(action1.name, action1.bindings[0].ToDisplayString());
  50.         UiManager.Instance.UpdateKeyText(action2.name, action2.bindings[0].ToDisplayString());
  51.         UiManager.Instance.UpdateKeyText(action3.name, action3.bindings[0].ToDisplayString());
  52.     }
  53.  
  54.     private void StartRebindAction(InputAction inputAction)
  55.     {
  56.         action = inputAction.ChangeBinding(0);
  57.         binding = action.To(inputAction.bindings[0]);
  58.         RemapButton(inputAction, binding);
  59.     }
  60.  
  61.  
  62.     private void StartRebindMove(int bindingIndex)
  63.     {
  64.         InputAction inputAction = gameplayActionMap.actions[0];
  65.         string moveDir = inputAction.bindings[bindingIndex].name;
  66.         action = inputAction.ChangeBinding("WASD");
  67.         binding = action.NextPartBinding(moveDir);
  68.         RemapMoveButton(inputAction, binding);
  69.  
  70.     }
  71.    
  72.     private void RemapMoveButton(InputAction inputAction, InputActionSetupExtensions.BindingSyntax bind)
  73.     {
  74.         var rebind = inputAction.PerformInteractiveRebinding();
  75.            
  76.         rebindingOperation = rebind.WithTargetBinding(bind.bindingIndex)
  77.             .WithControlsExcluding("Mouse")
  78.             .OnMatchWaitForAnother(0.1f)
  79.             .OnComplete(operation => RebindComplete(inputAction, bind.bindingIndex))
  80.             .Start();
  81.     }
  82.    
  83.     private void RemapButton(InputAction inputAction, InputActionSetupExtensions.BindingSyntax bind)
  84.     {
  85.         var rebind = inputAction.PerformInteractiveRebinding();
  86.            
  87.         rebindingOperation = rebind.WithTargetBinding(bind.bindingIndex)
  88.             .WithControlsExcluding("Mouse")
  89.             .OnMatchWaitForAnother(0.1f)
  90.             .OnComplete(operation => RebindComplete(inputAction))
  91.             .Start();
  92.     }
  93.  
  94.     private void RebindComplete(InputAction a)
  95.     {
  96.         UiManager.Instance.UpdateKeyText(a.name, a.bindings[0].ToDisplayString());
  97.         rebindingOperation.Dispose();
  98.     }
  99.    
  100.     private void RebindComplete(InputAction a, int bindingIndex)
  101.     {
  102.         UiManager.Instance.UpdateKeyText(char.ToUpper(a.bindings[bindingIndex].name[0]) + a.bindings[bindingIndex].name.Substring(1), a.bindings[bindingIndex].ToDisplayString());
  103.         rebindingOperation.Dispose();
  104.     }
  105.    
  106.     [UsedImplicitly]
  107.     public void KeybindOnClick(int index)
  108.     {
  109.         index++;
  110.         InputAction inputAction = gameplayActionMap.FindAction("Movement");
  111.         inputAction.Disable();
  112.         StartRebindMove(index);
  113.         inputAction.Enable();
  114.     }
  115.    
  116.     [UsedImplicitly]
  117.     public void KeybindOnClick(string actionName)
  118.     {
  119.         InputAction inputAction = gameplayActionMap.FindAction(actionName);
  120.         inputAction.Disable();
  121.         StartRebindAction(inputAction);
  122.         inputAction.Enable();
  123.     }
  124. }
Add Comment
Please, Sign In to add comment