Add keyboard input rebinding
This commit is contained in:
parent
91482820da
commit
4e96a88306
3 changed files with 3122 additions and 7 deletions
2
.erp
2
.erp
|
@ -5,7 +5,7 @@
|
||||||
<resetOnSceneChange>false</resetOnSceneChange>
|
<resetOnSceneChange>false</resetOnSceneChange>
|
||||||
<debugMode>false</debugMode>
|
<debugMode>false</debugMode>
|
||||||
<EditorClosed>true</EditorClosed>
|
<EditorClosed>true</EditorClosed>
|
||||||
<LastTimestamp>1677873349</LastTimestamp>
|
<LastTimestamp>1677880402</LastTimestamp>
|
||||||
<LastSessionID>7184246594242351147</LastSessionID>
|
<LastSessionID>7184246594242351147</LastSessionID>
|
||||||
<Errored>false</Errored>
|
<Errored>false</Errored>
|
||||||
</ERPSettings>
|
</ERPSettings>
|
File diff suppressed because it is too large
Load diff
|
@ -2,16 +2,29 @@ using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System;
|
using System;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.InputSystem;
|
||||||
|
|
||||||
public class GameInput : MonoBehaviour
|
public class GameInput : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private const string PLAYER_PREFS_BINDINGS = "InputBindings";
|
||||||
|
|
||||||
public static GameInput Instance { get; private set; }
|
public static GameInput Instance { get; private set; }
|
||||||
|
|
||||||
public event EventHandler OnInteractAction;
|
public event EventHandler OnInteractAction;
|
||||||
public event EventHandler OnInteractAlteranteAction;
|
public event EventHandler OnInteractAlteranteAction;
|
||||||
public event EventHandler OnPauseAction;
|
public event EventHandler OnPauseAction;
|
||||||
|
|
||||||
|
public enum Bindings
|
||||||
|
{
|
||||||
|
Move_Up,
|
||||||
|
Move_Down,
|
||||||
|
Move_Left,
|
||||||
|
Move_Right,
|
||||||
|
Interact,
|
||||||
|
InteractAlternate,
|
||||||
|
Pause
|
||||||
|
}
|
||||||
|
|
||||||
private PlayerInputActions playerInputActions;
|
private PlayerInputActions playerInputActions;
|
||||||
|
|
||||||
|
@ -21,6 +34,12 @@ public class GameInput : MonoBehaviour
|
||||||
Instance = this;
|
Instance = this;
|
||||||
|
|
||||||
playerInputActions = new PlayerInputActions();
|
playerInputActions = new PlayerInputActions();
|
||||||
|
|
||||||
|
if (PlayerPrefs.HasKey(PLAYER_PREFS_BINDINGS))
|
||||||
|
{
|
||||||
|
playerInputActions.LoadBindingOverridesFromJson(PlayerPrefs.GetString(PLAYER_PREFS_BINDINGS));
|
||||||
|
}
|
||||||
|
|
||||||
playerInputActions.Player.Enable();
|
playerInputActions.Player.Enable();
|
||||||
|
|
||||||
playerInputActions.Player.Interact.performed += Interact_performed;
|
playerInputActions.Player.Interact.performed += Interact_performed;
|
||||||
|
@ -60,4 +79,77 @@ public class GameInput : MonoBehaviour
|
||||||
|
|
||||||
return inputVector;
|
return inputVector;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string GetBindingText(Bindings bindings)
|
||||||
|
{
|
||||||
|
switch (bindings)
|
||||||
|
{
|
||||||
|
default:
|
||||||
|
case Bindings.Move_Up:
|
||||||
|
return playerInputActions.Player.Move.bindings[1].ToDisplayString();
|
||||||
|
case Bindings.Move_Down:
|
||||||
|
return playerInputActions.Player.Move.bindings[2].ToDisplayString();
|
||||||
|
case Bindings.Move_Left:
|
||||||
|
return playerInputActions.Player.Move.bindings[3].ToDisplayString();
|
||||||
|
case Bindings.Move_Right:
|
||||||
|
return playerInputActions.Player.Move.bindings[4].ToDisplayString();
|
||||||
|
case Bindings.Interact:
|
||||||
|
return playerInputActions.Player.Interact.bindings[0].ToDisplayString();
|
||||||
|
case Bindings.InteractAlternate:
|
||||||
|
return playerInputActions.Player.InteractAlternate.bindings[0].ToDisplayString();
|
||||||
|
case Bindings.Pause:
|
||||||
|
return playerInputActions.Player.Pause.bindings[0].ToDisplayString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RebindButton(Bindings bind, Action onActionRebound)
|
||||||
|
{
|
||||||
|
playerInputActions.Player.Disable();
|
||||||
|
|
||||||
|
InputAction inputAction;
|
||||||
|
int bindIndex;
|
||||||
|
|
||||||
|
switch (bind)
|
||||||
|
{
|
||||||
|
default:
|
||||||
|
case Bindings.Move_Up:
|
||||||
|
inputAction = playerInputActions.Player.Move;
|
||||||
|
bindIndex = 1;
|
||||||
|
break;
|
||||||
|
case Bindings.Move_Down:
|
||||||
|
inputAction = playerInputActions.Player.Move;
|
||||||
|
bindIndex = 2;
|
||||||
|
break;
|
||||||
|
case Bindings.Move_Left:
|
||||||
|
inputAction = playerInputActions.Player.Move;
|
||||||
|
bindIndex = 3;
|
||||||
|
break;
|
||||||
|
case Bindings.Move_Right:
|
||||||
|
inputAction = playerInputActions.Player.Move;
|
||||||
|
bindIndex = 4;
|
||||||
|
break;
|
||||||
|
case Bindings.Interact:
|
||||||
|
inputAction = playerInputActions.Player.Interact;
|
||||||
|
bindIndex = 0;
|
||||||
|
break;
|
||||||
|
case Bindings.InteractAlternate:
|
||||||
|
inputAction = playerInputActions.Player.InteractAlternate;
|
||||||
|
bindIndex = 0;
|
||||||
|
break;
|
||||||
|
case Bindings.Pause:
|
||||||
|
inputAction = playerInputActions.Player.Pause;
|
||||||
|
bindIndex = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
inputAction.PerformInteractiveRebinding(bindIndex).OnComplete(callback => {
|
||||||
|
playerInputActions.Player.Enable();
|
||||||
|
onActionRebound();
|
||||||
|
|
||||||
|
string playerOveridesJson = playerInputActions.SaveBindingOverridesAsJson();
|
||||||
|
PlayerPrefs.SetString(PLAYER_PREFS_BINDINGS, playerOveridesJson);
|
||||||
|
PlayerPrefs.Save();
|
||||||
|
}).Start();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue