Search Unity

Rebind - Bindable keys for your games [Released]

Discussion in 'Assets and Asset Store' started by Bladesfist, Oct 27, 2012.

  1. Bladesfist

    Bladesfist

    Joined:
    Jan 3, 2012
    Posts:
    107
    Hello.

    Rebind is a small plugin for Unity to support key bindings a very common feature in almost all PC releases. The main goal of Rebind is to make it just as easy for the developer to support key bindings. It was developed as a personal plugin for something I am working on however seeing as I could not find anything like it I decided to release it to you guys.

    What is it?
    An Input remapper for Unity3D.
    Rebind allows users to easily set up bindable keys for their game in the Unity Editor, via the Rebind class or by editing the config files that are produced. Rebind supports all forms of keyboard and mouse input and provides a simple layer on top of Unity's Input class to get Input from the binded keys.

    Features
    • Setup keys quickly and easily in the editor
    • Setup multiple keys per action
    • Edit key bindings in the editor, config files or at runtime
    • Get input just as easily as normal

    Setup
    Create a new GameObject and attach Rebind.cs to it.
    Inside Rebind.cs edit the ConfigFilePath with the directory you wish the files to be stored in.
    Code (csharp):
    1. public static void SetupSerializers()
    2.     {
    3.         ConfigFilePath = Application.dataPath + @"\Demo\Binds";
    4.         defaultBindsSerializer = new ConfigSerializer(ConfigFilePath + @"\DefaultBindings.txt");
    5.         keyBindsSerializer = new ConfigSerializer(ConfigFilePath + @"\KeyBindings.txt");
    6.     }
    Getting Input
    Getting Input is very easy. Instead of calling Input call Rebind and pass in your action name.
    Code (csharp):
    1. if (Rebind.GetInput("Forward"))
    2.     {
    3.         //Do stuff
    4.     }

    Setup of primary and secondary keys can be done in the editor. If you wish to have more keys bound to an action you can do so by editing the config files. The editor makes it very easy to add, delete and edit your actions.

    Download: https://bitbucket.org/Bladesfist/rebind
     
  2. SixTimesNothing

    SixTimesNothing

    Joined:
    Dec 26, 2007
    Posts:
    167
    Just want to say thanks for this tool!

    It was really helpful in creating a system that allows keys to be configured by the user in the game rather than via Unity's built in system which allows keys to be set at startup. It was easy enough to repurpose the custom inspector GUI for in game GUI.

    I also rewrote the whole package in JavaScript, as it was hard to integrate the existing C# system into a Unity JavaScript projects. I'd be happy to share this version if it would help others.
     
  3. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,820
    Would be greatly appreciated!

    jrDev
     
  4. SixTimesNothing

    SixTimesNothing

    Joined:
    Dec 26, 2007
    Posts:
    167
    Sure, I'll put it up when I get home tonight.

    FYI – I condensed it all into one script and I didn't bother to rebuild the custom inspector, as our game just required the ability to allow players to set keys at runtime – having it in the Unity editor wasn't really required. Though I imagine this would be pretty easy to add if you needed it.

    In my JS version, all user key settings are stored in KeyBindings.txt – DefaultBindings.txt is never changed. It's only used as a reference to allow the keys to be set back to the default.

    Note: I also fixed a bug with resetting the key bindings to the defaults: In the original version you can only reset the keys once each time the game is run. This happens because the code just sets the user keys Dictionary to the default keys Dictionary which is the same object. Thereafter, if you change anything in keys, it also changes it in defaultKeys, as they are effectively the same thing. Hope that makes sense! Anyway, it's fixed. :)
     
  5. SixTimesNothing

    SixTimesNothing

    Joined:
    Dec 26, 2007
    Posts:
    167
    Here you go: http://db.tt/tXWvOiCo

    I also added in the runtime GUI code with a quick demo – but stripped out any styling (just in case you;re wondering why it looks like that).

    Note again that I didn't rewrite the custom inspector, so the default keys have to be manually edited in KeyBindings.txt

    In fact when you distribute the runtime, KeyBindings.txt and DefaultBindings.txt should both be the same (i.e. the settings are set to the default).

    Also, to fix the aforementioned bug in the C# version:

    In Rebind/Dictionarys/Keys.cs change the ResetAllKeysToDefaults method (line 9 - 12) to the following (should be right, not tested though!):

    Code (csharp):
    1.  
    2. foreach (KeyValuePair<String, List<InputCode>> pair in Dictionary) {
    3.     int n = pair.Value.Count;
    4.     for (int i = 0; i < n; i++) {
    5.         pair.Value[i] = Rebind.defaultKeys.Dictionary[pair.Key][i];
    6.     }
    7. }
    8.  
     
    Last edited: Aug 26, 2013
  6. Crixu

    Crixu

    Joined:
    Aug 25, 2014
    Posts:
    21
    Hey,

    Looking into building a controls menu, but no idea how. Can't find any information for C# scripts :/
    Could you please help me, I really want to create a good controls menu.

    I would really appreciate it if you could help me.

    Thanks in advance!
    Crixu
     
  7. SixTimesNothing

    SixTimesNothing

    Joined:
    Dec 26, 2007
    Posts:
    167
    Original link is dead, and someone else asked me for it, so there's a copy of the original C# implementation here: https://db.tt/jYBsrDh4

    Note the aforementioned bug though, I don't recall off hand if it's already fixed in that version or if I just did in the JS one.
     
  8. Boi

    Boi

    Joined:
    Apr 10, 2010
    Posts:
    65
    Hi, is it possible to read the Input Mapping set in Unity's Input Manager? I want to be able to adapt my tutorial texts (in game) to whatever a player choose in this window:
     
  9. SixTimesNothing

    SixTimesNothing

    Joined:
    Dec 26, 2007
    Posts:
    167
    No it isn't possible – Unity does not expose that part of the Input namespace. You can't use script to get or set the mappings. That's why the OP created rebind in the first place!
     
  10. LazerTale

    LazerTale

    Joined:
    Apr 10, 2013
    Posts:
    6
    @SixTimesNothing, can you reup the JS version of the code!! Dropbox link is showing error!
     
  11. SixTimesNothing

    SixTimesNothing

    Joined:
    Dec 26, 2007
    Posts:
    167
  12. ashkechumblue

    ashkechumblue

    Joined:
    Mar 19, 2016
    Posts:
    1
    Hi question does it work with controllers