How to Rebind Your Controls in Unity (With Icons!) | Input System

Sdílet
Vložit
  • čas přidán 16. 07. 2024
  • Show your Support & Get Exclusive Benefits on Patreon (Including Access to this project's Source Files + Code) - / sasquatchbgames
    Join our Discord Community! - / discord
    By the end of this Unity Tutorial, you'll know how to setup controls with the new Input System, create an Input Manager for them, and have a fully working key-rebinding system that works for any device, and has an easy option to use Text OR icons for your re-binding UI.
    Link to Download the FREE 2D Asset Pack seen in this tutorial:
    veilofmaia.com/tutorial-asset...
    ---
    In need of some Unity Assets? Using our affiliate link is a great way to support us. It's free, and we get a small cut that helps keep us up and running: assetstore.unity.com?aid=1100lwgBQ
    ---
    Link to Download icons for basically ANY controller you could ever want:
    opengameart.org/content/free-...
    Link that goes into detail about the display options for the Rebinding system:
    docs.unity3d.com/Packages/com...
    Contents of This Video: ------------------------------------------
    00:00 - Intro
    00:20 - Setting up Controls and Detecting Input
    04:57 - Setting up the Rebinding UI
    08:00 - Getting our First Re-binding to work
    08:38 - Option to Cancel out of re-binding/Exclude Certain Controls
    09:50 - Adding a function to ignore re-bind duplicates
    12:54 - Adding the Option to Name our Action in the UI
    15:29 - How to Display Icons instead of Text
    17:19 - Reset bindings for a specific control scheme
    19:01 - Keep Re-bindings persistent
    Who We Are-------------------------------------
    If you're new to our channel, we're Brandon & Nikki from Sasquatch B Studios. We sold our house to start our game studio, and work full time on building our business and making our game, Veil of Maia.
    Wishlist our Games:
    Wishlist Samurado on Steam! - store.steampowered.com/app/23...
    Wishlist Veil of Maia! - store.steampowered.com/app/19...
    Don't forget to Subscribe for NEW game dev videos every Monday & Thursday!
    Follow us on Twitter for regular updates!
    / sasquatchbgames
    #unitytutorial #unity2d #unity3d
  • Hry

Komentáře • 64

  • @sasquatchbgames
    @sasquatchbgames  Před rokem +35

    A bug was found where, if you separate a composite binding (ie. A vector2 up/down/left/right binding) into 4 separate bindings that you want to rebind, a duplication was possible within the composite binding itself.
    To fix this, you'll need to change the CheckDuplicateBindings functions to the following:
    private bool CheckDuplicateBindings(InputAction action, int bindingIndex, bool allCompositeParts = false)
    {
    InputBinding newBinding = action.bindings[bindingIndex];
    int currentIndex = -1;
    foreach (InputBinding binding in action.actionMap.bindings)
    {
    currentIndex++;
    if (binding.action == newBinding.action)
    {
    if (binding.isPartOfComposite && currentIndex != bindingIndex)
    {
    if (binding.effectivePath == newBinding.effectivePath)
    {
    Debug.Log("Duplicate binding found in composite: " + newBinding.effectivePath);
    return true;
    }
    }
    else
    {
    continue;
    }
    }
    if (binding.effectivePath == newBinding.effectivePath)
    {
    Debug.Log("Duplicate binding found: " + newBinding.effectivePath);
    return true;
    }
    }
    if (allCompositeParts)
    {
    for (int i = 1; i < bindingIndex; i++)
    {
    if (action.bindings[i].effectivePath == newBinding.overridePath)
    {
    //Debug.Log("Duplicate binding found: " + newBinding.effectivePath);
    return true;
    }
    }
    }
    return false;
    }
    This also affects the "reset" button, so you'll need to change the ResetBinding function to the below as well:
    private void ResetBinding(InputAction action, int bindingIndex)
    {
    InputBinding newBinding = action.bindings[bindingIndex];
    string oldOverridePath = newBinding.overridePath;
    action.RemoveBindingOverride(bindingIndex);
    int currentIndex = -1;
    foreach (InputAction otherAction in action.actionMap.actions)
    {
    currentIndex++;
    InputBinding currentBinding = action.actionMap.bindings[currentIndex];
    if (otherAction == action)
    {
    if (newBinding.isPartOfComposite)
    {
    if (currentBinding.overridePath == newBinding.path)
    {
    otherAction.ApplyBindingOverride(currentIndex, oldOverridePath);
    }
    }
    else
    {
    continue;
    }
    }
    for (int i = 0; i < otherAction.bindings.Count; i++)
    {
    InputBinding binding = otherAction.bindings[i];
    if (binding.overridePath == newBinding.path)
    {
    otherAction.ApplyBindingOverride(i, oldOverridePath);
    }
    }
    }
    }
    And finally, to ensure you can use either WASD separately OR together, be sure to update the ResetToDefault method to:
    public void ResetToDefault()
    {
    if (!ResolveActionAndBinding(out var action, out var bindingIndex))
    return;
    ResetBinding(action, bindingIndex);
    if (action.bindings[bindingIndex].isComposite)
    {
    // It's a composite. Remove overrides from part bindings.
    for (var i = bindingIndex + 1; i < action.bindings.Count && action.bindings[i].isPartOfComposite; ++i)
    action.RemoveBindingOverride(i);
    }
    else
    {
    action.RemoveBindingOverride(bindingIndex);
    }
    UpdateBindingDisplay();
    }

  • @manningfamilychronicles1601
    @manningfamilychronicles1601 Před 11 měsíci +11

    This is no joke right here. Really daunting going through this. Well done. I'm glad I went through this, and I really appreciate the hard work that went into putting this together.

  • @Laumania
    @Laumania Před 10 měsíci +1

    This is one perfect video! Thank you. Saving it for when I’m redoing my ui and will add rebinding :)

  • @adassus
    @adassus Před 3 měsíci +1

    Thank you for your efforts. Your channel is fast becoming my go to place for quality tutorials full of good practices.

  • @thankyouverymuch5743
    @thankyouverymuch5743 Před rokem +1

    Thank you so much, I was searching for this all day.

  • @tornadre
    @tornadre Před 10 měsíci +2

    Thank you for putting this video together. Really helped me out and I really enjoyed your method of tutorial making. I've subbed and will be checking out your other work!

  • @castlecodersltd
    @castlecodersltd Před 3 měsíci +1

    Another really good/helpful tutorial from you. Thank you both 🙂

  • @elesh3178
    @elesh3178 Před 2 měsíci

    This is such a life saving tutorial. Many thanks!

  • @matt78324
    @matt78324 Před 11 měsíci +1

    Awesome video! Subscribed!

  • @angelrubiov
    @angelrubiov Před 11 měsíci +1

    Thanks so much for this tutorial, it worked for me and I was extremely lost haha

  • @amirhebadatiyaan
    @amirhebadatiyaan Před 5 měsíci +1

    Thanks for the great tutorial!!!

  • @aditya90m
    @aditya90m Před rokem +4

    Hey, thanks for the great tutorial. I set up my input system differently, thought I'd share.
    Instead of using the UI based Input Action control binding, I did it all in code. I set up an "InputAction" for each action, and used the "AddBinding or "AddCompositeBinding" function to bind keys/buttons to the action. This way, I can store my control binding map in a json file and then read it and load the controls easily.
    For using these actions, I pass a function definition as an "InputAction.CallbackContext" and add it to action.performed and (optionally) action.canceled. This way, the function gets triggered directly when any action is performed. So, I don't need the bool variables to detect when an action has been performed. The triggered function will receive the context and you can detect whether the action was performed or canceled from the context.
    I wasn't sure out how to do the re-bindings and stuff, but your tutorial has given me a good idea. Thanks again!

  • @pengchengliu7648
    @pengchengliu7648 Před 3 měsíci +1

    This is realy helpful, thank you very much!

  • @INeatFreak
    @INeatFreak Před rokem +1

    Great video 👍

  • @codem7173
    @codem7173 Před 10 měsíci +1

    Great tutorial, ty

  • @fleity
    @fleity Před 2 měsíci

    Very nice, very comprehensive overview of how to so rebinding thank you so much.
    In the beginning you mention rebinding is easier with player input than generated c# classes, but not say why though?

  • @AnEmortalKid
    @AnEmortalKid Před rokem +2

    For Ui icons, I used Xelu’s controller prompts

  • @pocket-logic1154
    @pocket-logic1154 Před rokem +2

    Just a tip: You can also press Ctrl + E + C to comment out lines of code, making it a bit easier to reach the keys with one hand in one single motion ;-)

  • @MasterDisaster64
    @MasterDisaster64 Před rokem +4

    Thanks a lot for this!
    Just wondering, how hard would it be to modify this so you can add several bindings to an action instead of just one?

  • @wolflexx
    @wolflexx Před 3 měsíci

    Thank you

  • @AnEmortalKid
    @AnEmortalKid Před rokem

    Omg.. i just implemented all of this myself not even knowing there were samples in the Unity package SMH. Learned a thing or two.

  • @hikineet9673
    @hikineet9673 Před 6 dny

    Thanks

  • @treyhayden4261
    @treyhayden4261 Před rokem +4

    The duplicate rebind modifications were almost identical to samyam's video. :\

  • @sethdossett1304
    @sethdossett1304 Před rokem

    I was actually gonna ask you how you were doing this in your games haha

  • @IAutumnWest
    @IAutumnWest Před 3 měsíci

    God bless this video. Absolute hero shit : - )

  • @sergiovieira4869
    @sergiovieira4869 Před měsícem

    Great Video! Can you explain why this method of rebinding won't work if you use C# generated class?

  • @ReidWinchester
    @ReidWinchester Před 23 dny +1

    the reset function is not working for me. i've double checked my RebindActionUI prefab reset button and it's set to call the resettodefault function when the player clicks, but for some reason, nothing happens when I press the button. I've got the actual rebinding portion of the tutorial done, but the reset buttons, which were apparently supposed to work out of the box, just are not working for me. Any advice? I followed the code for the reset function exactly

  • @benjaminsettlemire7006
    @benjaminsettlemire7006 Před 10 měsíci +1

    Do I have to use the Input System plugin or can I use the default unity input manager

  • @fplayfplay3161
    @fplayfplay3161 Před 8 měsíci

    Thanks for the tutorial.
    The last part, saving rebinding, getting:
    'InputActionAsset' does not contain a definition for 'SaveBindingOverridesAsJson' and no accessible extension method 'SaveBindingOverridesAsJson' accepting a first argument of type 'InputActionAsset' could be found (are you missing a using directive or an assembly reference?)
    any idea?

  • @SoggettoRava
    @SoggettoRava Před 8 měsíci

    Hi everyone!
    First of all, great video, it really helped me set up the rebinding options for my game! Still, I need help with a curious issue.
    Since I put all key reset into one single button, I managed to modify CheckDuplicateBinding function to integrate the swapping method you describe. This way, new bindings immediately swap with duplicates, if found. And everything looks like it's working fine.
    Now, I encountered a small issue I'm not sure how to solve, so I'm hoping anyone here could point me in the right direction...
    As I said, rebinding perfectly works as long as I stay in the Editor. When I build the game, however, something weird happens: I can rebind keys without any trouble, but if I close the pause menu (both with escape and by selectin "resume") and then reopen it, the rebind is maintained, but the controls list displays the original, default keys.
    Example: M key opens map; I rebind the function to key G; now the list says MAP - G and if I close menu and test, G key opens map (perfect); now, if I reopen the menu, it says MAP - M (original binding), but the G key is still the one performing the action.
    So, to wrap up, the rebinding works perfectly but it DISPLAYS incorrectly... and only in the build, not in the editor, where new keys are displayed correctly. Any idea on what changes between editor and build? Or on where I should go looking for a solution?
    Thanks!

  • @animedreammachine7123

    Hey man could you do a Steam Deck controls video? Thanks man love the videos

    • @sasquatchbgames
      @sasquatchbgames  Před rokem +1

      I need to get myself a Steam Deck first. Thanks for the idea!

  • @LilianCRE
    @LilianCRE Před 4 dny +1

    Hello i'm actualy making a fpv drone video game and i want us to be able to use any radio transmiter. In most of FPV Simulator you have a system call the joystick calibration where you have to move your joystick and the game listen to the input of your controller and bind it. I want to do the same system into my game but i realy don't know how so if somebody know how to do it i will realy apreciat it. Thanx

  • @BarelyTryingStudio
    @BarelyTryingStudio Před 4 měsíci

    Hopefully you see you this, I was just wondering, if You assigned those sprites for xbox but if the player only has a PlayStation controller wouldn't the player only see xbox controls until they rebind ? is there a way to set the default inputs based on what controller gets detected ?

  • @lacrime_khalil3032
    @lacrime_khalil3032 Před 6 měsíci

    I need help! Everything works fine but in my game my player's jump height is variable on jump key Being held but so i changed the jump input is userinput but now my player jumps by itself when jump key is held, which is not what i want

  • @JagGentlemann
    @JagGentlemann Před 9 měsíci

    I updated the Input System so everything is on point, but the keyboard stopped working with the Action Map I use for the gameplay. I don't want to do all of this without updating, because I know it'll be worse in the future, and the listen function doesn't even work for me in the old version.

  • @LordBete
    @LordBete Před měsícem

    Followed every step, but when it comes to testing the rebind, the rebindUI is updated with my new keys but my character still controls using WASD.
    I assume this is because I have generated the C# class you said not to, but I need that class. What do we do in that case?

  • @tommyyodergamecreator
    @tommyyodergamecreator Před 11 měsíci

    Found a bug and I am not sure how to fix it. Let's say you have one button set to R2 and another set to L2. If you remap the button with R2 to X and proceed to switch the second button to R2. When you go to switch the first button back to R2 the font updates but Waiting For Responses is still visible. It would be neat to add the set of code to swap like when you reset but nothing happens if I add action.RemoveBindingOverride(bindingIndex); to PerformInteractiveRebind during check for duplicates nothing happens. Any advice would be great.

  • @CoderDev6545
    @CoderDev6545 Před 5 měsíci +1

    I found an issue where if you cancel the rebinding and the binding contains composits, the changes will not get revered and will stay as is. This causes a duplication glitch that I've tried fixing myself, but could not fix. I also found an issue where if you rebind an action, then put in a duplicate, it resets the value back to its original value before the rebinding. However, I found a solution to this error by putting in a variable to remember what is was before the method is performed, so it can revert to that by using "action.ApplyBindingOverride(bindingIndex, _binding);". _binding is the variable that stores the previous value, which you can set before the operation is called: string _binding = action.bindings[bindingIndex].effectivePath;. Can you please help me with the original problem with that being canceling a rebinding that contains composits (which don't revert the changes)? Thank you!

  • @yihongzheng1142
    @yihongzheng1142 Před rokem

    The button binding is successful, but it is not mapped to the character

  • @AlesVokrinek
    @AlesVokrinek Před 4 měsíci

    Good tutorial but i for some reason cant rebind like jump to left mouse button. Can you please help me.

  • @yihongzheng1142
    @yihongzheng1142 Před rokem

    I copy the code step by step and it still can't work in the end ,i don't know why ?😭

  • @chunsh10
    @chunsh10 Před 24 dny

    I can't see Unity.TextMeshPro assembly at 6:31. Do you know how to resolve this?

  • @QWERTZ-NOOB
    @QWERTZ-NOOB Před 3 měsíci

    My question is not entirely related to this video but maybe somebody can still shine a light on it for me. Can I use Unity to create a tool that remaps and/or automates controller input? I've made some makros with AHK but I'd rather switch to something more advanced and polished that also uses a C-style language because I don't really like the syntax of AHK and its documentation is very bad. I know that Python is the first choice for tasks like this in the Open Source community but I don't really like that language either.

  • @ColtPtrHun
    @ColtPtrHun Před rokem

    4:40
    Line 97 I think the walk checking is wrong

  • @barionlp
    @barionlp Před měsícem

    Why does no one use UI Toolkit :(

  • @readyok8230
    @readyok8230 Před rokem

    Hey there, thanks for this video. I noticed that .WithCancelingThrough ("/escape") will also cancel when you press the "E" key on the keyboard. How would you avoid this? Thanks.

    • @AnEmortalKid
      @AnEmortalKid Před rokem

      That’s a unity bug. Lemme find you the code for this one sec.

    • @readyok8230
      @readyok8230 Před rokem

      @@AnEmortalKid Hey there, thanks. The solution is to upgrade to input system 1.4.1

    • @AnEmortalKid
      @AnEmortalKid Před rokem +1

      @@readyok8230 omg lol. Ok I shall check if I have the latest.

  • @NiKoClemes
    @NiKoClemes Před 6 měsíci

    8:00

  • @martinchya2546
    @martinchya2546 Před rokem +2

    Hello. The thing in 4:00 technically is not a singleton. Its a global static reference that self-manages. For it to be singleton, it should automatically instantiate itself. Just a minor nitpick.

  • @Coco-gg5vp
    @Coco-gg5vp Před rokem

    First

  • @flecsodev4210
    @flecsodev4210 Před 3 měsíci

    Don't forget to change inputs in settings otherwise this doesn't work

  • @justbake7239
    @justbake7239 Před 3 měsíci +2

    it really annoys me when I find a good tutorial but it doesn't stick to good programming practices.

    • @Luis-Torres
      @Luis-Torres Před 3 měsíci +2

      Most of the code was done by Unity themselves in this video and was edited by Brandon. I'm curious what the issues you had with the programming practices? 🤔

    • @justbake7239
      @justbake7239 Před 3 měsíci +1

      @@Luis-Torres As much as i would like to explain doing so in a youtube comment is a bit out of scope. For future reference though don't assume that unity always follows good programming practices.

    • @Luis-Torres
      @Luis-Torres Před 3 měsíci +10

      @@justbake7239 I don't assume Unity follows best practices...but that's not the point 😅
      Your comment seems to be criticizing the video itself when the reality is that the criticism should be aimed at Unity if at all. If you're going to make a critique about programming practices, then elaborate on that. If you decide not to, then the comment seems kind of pointless to me.
      I was hoping to learn something from this comment.

    • @SekGuy
      @SekGuy Před měsícem +1

      ​@@justbake7239well, a lot of the time if it is out of the scope for a comment it is also out of scope for the video...

  • @sebastianhall8150
    @sebastianhall8150 Před rokem

    First