Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How would I go about making a custom input manager?

Discussion in 'General Discussion' started by MadJohny, Jul 25, 2014.

  1. MadJohny

    MadJohny

    Joined:
    Mar 8, 2013
    Posts:
    143
    Hello, I am currently making a local multiplayer game, and I plan it on being played up to 4 controllers. The thing is, the input manager in unity is kinda unorganized and it's also slow to work with (if I want to add that much button, counting 12 buttons for each player + the joysticks). So is there anyway to do it via script? Like doing

    Code (CSharp):
    1.     public Player1Input player1Input;
    2.     [System.Serializable]
    3.     public class Player1Input {
    4.         KeyCode JumpButton;
    5.         //etc.
    6.     }
    And then it's a matter of copying and pasting for each player, and then in the beginning of the game, each player would click any button, and it would assign to which class the player should use? Get what I mean? Any tips on how to achieve this?

    Thanks in advance.
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,715
    KellyThomas likes this.
  3. MadJohny

    MadJohny

    Joined:
    Mar 8, 2013
    Posts:
    143
    I'm not really into these kinda of packages because I like to know what's written in the scripts, but on another side, I don't like to read already made scripts. I would prefer making a solution like the one I stated above, but I don't know how to start.
     
  4. KellyThomas

    KellyThomas

    Joined:
    Jul 1, 2012
    Posts:
    39
    I'm going to also suggest InControl, it is a quality solution with a lot of edge cases already taken care of.

    A slightly nerfed version is available gratis from github.

    It comes with out of the box support for many types of controller, and is easily extended if required.
     
  5. MadJohny

    MadJohny

    Joined:
    Mar 8, 2013
    Posts:
    143
    Hmm I dunno, I might take a look at inControl since it's the second time that someone suggests me it, but seriously, nothing about my option? ;p
     
  6. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    Go with inControl. It's your best option.
     
  7. MadJohny

    MadJohny

    Joined:
    Mar 8, 2013
    Posts:
    143
    Okay I guess, inControl it is.
     
  8. Deleted User

    Deleted User

    Guest

    I made a simple custom input manager as like you I prefer to learn stuff myself and do it. Surely those solutions mentioned are perfect for what you want to achieve but if you are in for learning I can give you some info on how I did my manager.

    What I simply do is arrange a List for key names and another List for the KeyCodes in a static class. In this class I have functions similar to the default Input Manager as Input.GetButonDown(string) for example, but instead of searching trough unity manager I search trough the List of key names, retrieve the index by name, and then apply the KeyCode of ther other List by index. Example:

    Code (csharp):
    1.  
    2.     static private List<string> keyName = new List<string>();  
    3.      static public List<KeyCode> keyCode = new List<KeyCode>();    
    4.  
    5.      static public bool GetKeyDown (string k) {
    6.        if (UnityEngine.Input.GetKeyDown(keyCode[keyName.IndexOf(k)]))
    7.          return true;  
    8.        else
    9.          return false;
    10.      }
    11.  
    As you can see it still uses Unity GetKey, which is different from Button as this uses just direct KeyCodes. Obiviously all the key inputs must be set at runtime before searching for them, so in this case you have to set first a key name to the first List and the KeyCode on the second in index order.

    So in your case with the players input you can have a series of predefined set of keycodes and check for them, maybe adding a generic class you add to each player. But you will still have a problem, ddealing with axis. For my system I made a separate class that return sensitivity and gravity, if you want I'm open to share my code trough pm and you can use as you see fit.
     
  9. MadJohny

    MadJohny

    Joined:
    Mar 8, 2013
    Posts:
    143
    Could you send me the whole search code so that I can take a look at it?
     
  10. Deleted User

    Deleted User

    Guest

    Sent you a message.
     
  11. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    That's a very simple, but nice solution. I would defiantly use a built in array instead of a list, for a little more speed.
     
  12. Deleted User

    Deleted User

    Guest

    Yeah an array would be more ideal performance wise, I did with List for a reason of comfort, assigning key binds is easier and fast, but I never tested with many keys. With around 20 key inputs and 6 axis is still all good performance wise, with the case of the OP with many inputs I'm not sure if an unique manager could cut many inputs at once. For a sepcific use like for me is good enough for a general purpose framework need more work.
     
  13. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Ya, I feel you! Had to go through and rework all the List<> in my project to get some(a lot) of speed. Not fun, but in my case was worth it. Not sure how much you'd get out of this, probably not nearly as much.
     
  14. Deleted User

    Deleted User

    Guest

    Yeah but is always good to remember for an "escape plan" if things start to act ugly.
     
  15. jaybennett

    jaybennett

    Joined:
    Jul 10, 2012
    Posts:
    165
    I would use a Dictionary<string, KeyCode> to map the buttons.

    Code (csharp):
    1.  
    2.     static private Dictionary<string, KeyCode> buttonMap = new Dictionary<string, KeyCode>();
    3.  
    4.      static public bool GetKeyDown (string k) {
    5.        if (UnityEngine.Input.GetKeyDown(buttonMap[k]))
    6.          return true;
    7.        else
    8.          return false;
    9.  
    Just a bit simpler IMO.

    There is no reason to use a built in array for this. The list will only change at startup or when the client changes button settings.
     
  16. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    While it might not be needed, the built in arrays are much faster and like you said, they only change on startup, therefore there is no reason not to use the built in array for the extra speed.
     
  17. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    2,980
    I would also suggest InControl. It works great with game controllers.