-
Notifications
You must be signed in to change notification settings - Fork 0
Main Idea
Collapsed Plug edited this page Aug 5, 2022
·
1 revision
The InputManager wraps Unity's New Input System so that we can handle
key down, up and hold events using listeners (we call them delegates here.)
There are three main components that play important roles for InputManager.
- The InputManager itself.
- You will use this in your main game code to trigger the delegates.
- Action Enum.
- The delegates will receive a member of this enum.
It corresponds to New Input System's "Action Map" while its members are for Actions.
- The delegates will receive a member of this enum.
- Action to Enum Relation Definition
- This connects the enum members with the Actions, and it is a Scriptable Object you can edit in Unity Editor.
This code shows an example implementation of MonoBehaviour. The action enum for this code is SampleKeys
and it has two members: Action and Rebind.
using InputManager.Domain;
using UnityEngine;
public class YourMB: MonoBehaviour {
// Your InputManager should be put into the interface (IInputManager<T>)
// where T is the enum you have created.
private IInputManager<SampleKeys> _inputManager;
// Put your key settings asset through Unity Editor.
public SampleKeySettings keySettings;
private void Start() [
// Put your input manager instance with the key setting asset.
_inputManager = new InputManager<SampleKeys>(keySettings);
// Your keydown event listener must take your enum (in this case, SampleKeys. See OnKeyDown below.)
_inputManager.AddOnKeyDownDelegate(OnKeyDown);
}
private void Update() {
// THIS IS IMPORTANT! Without this, your input manager won't work!!
_inputManager.CheckKey();
}
// This is your key down event listener. As stated before, it must take your enum.
private void OnKeyDown(SampleKeys k) {
switch (k) {
case SampleKeys.Action:
Debug.Log("Action key pressed!");
break;
case SampleKeys.Rebind:
Debug.Log("Rebind key pressed!");
break;
}
}
}