How to Rebind Your Controls in Unity (With Icons!) | Input System
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
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();
}
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.
This is one perfect video! Thank you. Saving it for when I’m redoing my ui and will add rebinding :)
Thank you for your efforts. Your channel is fast becoming my go to place for quality tutorials full of good practices.
Thank you so much, I was searching for this all day.
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!
Another really good/helpful tutorial from you. Thank you both 🙂
This is such a life saving tutorial. Many thanks!
Awesome video! Subscribed!
Thanks so much for this tutorial, it worked for me and I was extremely lost haha
Thanks for the great tutorial!!!
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!
This is realy helpful, thank you very much!
Great video 👍
Great tutorial, ty
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?
For Ui icons, I used Xelu’s controller prompts
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 ;-)
i had no idea, thanks!
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?
Thank you
Omg.. i just implemented all of this myself not even knowing there were samples in the Unity package SMH. Learned a thing or two.
Thanks
The duplicate rebind modifications were almost identical to samyam's video. :\
I was actually gonna ask you how you were doing this in your games haha
God bless this video. Absolute hero shit : - )
Great Video! Can you explain why this method of rebinding won't work if you use C# generated class?
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
Do I have to use the Input System plugin or can I use the default unity input manager
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?
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!
Hey man could you do a Steam Deck controls video? Thanks man love the videos
I need to get myself a Steam Deck first. Thanks for the idea!
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
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 ?
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
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.
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?
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.
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!
The button binding is successful, but it is not mapped to the character
Good tutorial but i for some reason cant rebind like jump to left mouse button. Can you please help me.
I copy the code step by step and it still can't work in the end ,i don't know why ?😭
I can't see Unity.TextMeshPro assembly at 6:31. Do you know how to resolve this?
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.
4:40
Line 97 I think the walk checking is wrong
Why does no one use UI Toolkit :(
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.
That’s a unity bug. Lemme find you the code for this one sec.
@@AnEmortalKid Hey there, thanks. The solution is to upgrade to input system 1.4.1
@@readyok8230 omg lol. Ok I shall check if I have the latest.
8:00
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.
First
Don't forget to change inputs in settings otherwise this doesn't work
it really annoys me when I find a good tutorial but it doesn't stick to good programming practices.
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? 🤔
@@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.
@@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.
@@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...
First