Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using JetBrains.Annotations;
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class KeybindManager : MonoBehaviour
- {
- private InputActionRebindingExtensions.RebindingOperation rebindingOperation;
- private InputActionSetupExtensions.BindingSyntax action;
- private InputActionSetupExtensions.BindingSyntax binding;
- [SerializeField] private InputActionAsset inputActions;
- private InputActionMap gameplayActionMap;
- private InputAction action1;
- private InputAction action2;
- private InputAction action3;
- private static KeybindManager instance;
- public static KeybindManager Instance
- {
- get
- {
- if (instance == null)
- instance = FindObjectOfType<KeybindManager>();
- return instance;
- }
- }
- private void Awake()
- {
- gameplayActionMap = inputActions.FindActionMap("Default");
- action1 = gameplayActionMap.FindAction("Action 1");
- action2 = gameplayActionMap.FindAction("Action 2");
- action3 = gameplayActionMap.FindAction("Action 3");
- }
- private void Start()
- {
- var bindings = gameplayActionMap.actions[0].bindings;
- for (int i = 1; i < bindings.Count; i++)
- UiManager.Instance.UpdateKeyText(char.ToUpper(bindings[i].name[0]) + bindings[i].name.Substring(1) , bindings[i].ToDisplayString());
- UiManager.Instance.UpdateKeyText(action1.name, action1.bindings[0].ToDisplayString());
- UiManager.Instance.UpdateKeyText(action2.name, action2.bindings[0].ToDisplayString());
- UiManager.Instance.UpdateKeyText(action3.name, action3.bindings[0].ToDisplayString());
- }
- private void StartRebindAction(InputAction inputAction)
- {
- action = inputAction.ChangeBinding(0);
- binding = action.To(inputAction.bindings[0]);
- RemapButton(inputAction, binding);
- }
- private void StartRebindMove(int bindingIndex)
- {
- InputAction inputAction = gameplayActionMap.actions[0];
- string moveDir = inputAction.bindings[bindingIndex].name;
- action = inputAction.ChangeBinding("WASD");
- binding = action.NextPartBinding(moveDir);
- RemapMoveButton(inputAction, binding);
- }
- private void RemapMoveButton(InputAction inputAction, InputActionSetupExtensions.BindingSyntax bind)
- {
- var rebind = inputAction.PerformInteractiveRebinding();
- rebindingOperation = rebind.WithTargetBinding(bind.bindingIndex)
- .WithControlsExcluding("Mouse")
- .OnMatchWaitForAnother(0.1f)
- .OnComplete(operation => RebindComplete(inputAction, bind.bindingIndex))
- .Start();
- }
- private void RemapButton(InputAction inputAction, InputActionSetupExtensions.BindingSyntax bind)
- {
- var rebind = inputAction.PerformInteractiveRebinding();
- rebindingOperation = rebind.WithTargetBinding(bind.bindingIndex)
- .WithControlsExcluding("Mouse")
- .OnMatchWaitForAnother(0.1f)
- .OnComplete(operation => RebindComplete(inputAction))
- .Start();
- }
- private void RebindComplete(InputAction a)
- {
- UiManager.Instance.UpdateKeyText(a.name, a.bindings[0].ToDisplayString());
- rebindingOperation.Dispose();
- }
- private void RebindComplete(InputAction a, int bindingIndex)
- {
- UiManager.Instance.UpdateKeyText(char.ToUpper(a.bindings[bindingIndex].name[0]) + a.bindings[bindingIndex].name.Substring(1), a.bindings[bindingIndex].ToDisplayString());
- rebindingOperation.Dispose();
- }
- [UsedImplicitly]
- public void KeybindOnClick(int index)
- {
- index++;
- InputAction inputAction = gameplayActionMap.FindAction("Movement");
- inputAction.Disable();
- StartRebindMove(index);
- inputAction.Enable();
- }
- [UsedImplicitly]
- public void KeybindOnClick(string actionName)
- {
- InputAction inputAction = gameplayActionMap.FindAction(actionName);
- inputAction.Disable();
- StartRebindAction(inputAction);
- inputAction.Enable();
- }
- }
Add Comment
Please, Sign In to add comment