Input Manager
for Unity
Link to Assetstore page
The Input Manager is the basics for managing input. Its goal is to keep input fast and simple for unity developers.
Step by Step Tutorial:
This little tutorial should help you using the Input Manager. But first we should take a look at the code
Classes:
The Input Manager consists of 3 Classes (and an Enumeration which we will also put in here):InputManager | This is the main input manager. It is derived from MonoBehaviour, which means it can be added as a component to a GameObject. If everything goes as planned you should only be required to access this class at startup |
BindingGroup | This class just represents a group of Bindings. Its there so you can group your inputs. For example you have a group "UI" and a group "Player" |
Binding | This is the Binding that you will work with. It can be bound to a Key, Button or Axis. It has events that you can use to trigger actions when something changes about this Binding |
BindingType | This is an enum that represents the type of the Binding. It can be bound to a Key, an Axis or to nothing |
A complete code documentation can be found here
The Code:
Lets start with a normal c# script generated by Unitypublic class InputManagerDemo : MonoBehaviour { void Start(){ } void Update(){ } }
Now lets put in a InputManager variable and initialize it in Start
public class InputManagerDemo : MonoBehaviour { private InputManager inputManager; void Start(){ inputManager = this.gameObject.AddComponent(); } void Update(){ } }
Now we have our instance and only have to prepare some simple inputs. To keep this tutorial short we will only add two inputs and put them in one group each
public class InputManagerDemo : MonoBehaviour { private InputManager inputManager; BindingGroup GUIControls; Binding GUIShow; BindingGroup gameControls; Binding GUIHide; void Start(){ inputManager = this.gameObject.AddComponent(); GUIShow = new Binding ("GUIShow"); GUIShow.Keys = new KeyCode[]{KeyCode.Escape}; GUIShow.StateChanged += HandleStateChangedGUIShow; GUIHide = new Binding ("GUIHide"); GUIHide.Keys = new KeyCode[]{KeyCode.Mouse0}; GUIHide.StateChanged += HandleStateChangedGUIHide; GUIControls = new BindingGroup (); GUIControls.Add (GUIHide); gameControls = new BindingGroup (); gameControls.Add (GUIShow); inputManager.Add(GUIControls); inputManager.Add(gameControls); } void HandleStateChangedGUIShow (Binding sender, bool oldState, bool newState) { if (oldState && !newState) { //key up gameControls.Enabled = false; GUIControls.Enabled = true; } } void HandleStateChangedGUIHide (Binding sender, bool oldState, bool newState) { if (oldState && !newState) { //key up gameControls.Enabled = true; GUIControls.Enabled = false; } } void Update(){ } }
We have now setup our input manager with some bindings. These can now either be invidually activated or deactivated, or through the group that they are in. The current setup can be used to display and hide a menu. The corresponding actions can be put into the handlers of the StateChange event. If you want to use an Axis you can use the ValueChanged event. If you make the InputManager instance a singleton, you can access your Bindings by their name with InputManager.GetBinding(String name). For a more sophisticated Demo please look at the demo that is included with the asset.