How to Create a Custom Input Manager in Unity C#

Recently, I created a custom input manager for a game I’m making in Unity 3D. Initial research suggested I use Unity’s built-in Input Manager. However, Unity doesn’t have an API to access most of the Input Manager functionality. However, I wanted a menu in my game where the user could set their own key bindings; a common feature in most PC games. Unity’s Input Manager can only be accessed when you first start a game. I found this to be limiting. I want my players to be able to change their bindings whenever they want, during the game. So I decided to create my own input manager. Not much information was available to help me to accomplish this. After a lot of research and piecing things together, I was able to create a slightly rough, but functional, input manager for my game.

So I am sharing what I learned. This blog details how to create a custom input manager. Likewise, the video below provides an in-depth tutorial I made for the project. NOTE: Video assumes a moderate level of Unity experience. If you need to brush up on Unity basics, view here.

Lastly, if you want to check the project out, you can download it from here.

STEP 1 to Create Custom Input Manager: Make a Game Manager

Here is where we begin creating the custom input manager. First, create a Game Manager that will hold keycodes for our character movement. This Manager will load the user’s bindings from PlayerPrefs whenever the game starts.

STEP 4 to Create Custom Input Manager: Set up the Buttons

At this point, we need to make sure our buttons are doing something when we click them.

Left-click on ForwardKey button in hierarchy. In the inspector, click the + icon under the OnClick() section two times.

Drag the Canvas object from the hierarchy and into the object field for each behavior.

Drag the ForwardKey child Text object to the Text object in the OnClick() function.

Type “forward” in the string field for the second OnClick() behavior in the list.

Repeat this for the next 4 buttons. Be sure that each Text component is the child Text component from the button you are modifying. Likewise, each string for the second behavior should reflect a case from the MenuScript script. Example: forward, backward, left, right, jump. Ideally, the BackwardKey button should send backward in the StartAssignment call. LeftKey should send left, RightKey should send right, etc.

Once that’s all done, you should be able to play the game. Press Escape to open the panel. Left click on any of the buttons and then press a key to change the assigned Keycode. You should see the text on the button change to the new key and your new key should control the aspect of the movement that you changed. Example: If you change jump to Q, you should now be able to jump using the Q key.

Conclusion

In conclusion, I realize this may not be the ideal way to create a custom input manager. The functionality works fine if you have few commands. However, if you have a game with, say, 30 commands, it would be incredibly tedious and hard to keep track of all the code, buttons, and parameters you would be sending. In general, this tutorial covers the basics of setting up your own keys for your custom input manager. I figured that anybody reading this would likely be good enough with code to be able to figure out a more dynamic alternative using Arrays or some other variable that can be more readily automated. Hopefully this provides you with the basic groundwork to make your custom input manager more complex to fit the necessities of your game.