Key Rebinding with Complete Menu | Unity Input System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey fellow developers welcome to the tutorial on how to add a key binding menu to your Yuri project in this tutorial we are first going to import the unity input system then we will create a simple scene with a character controlled by the input system after this we will set up our UI for key binding menu next we Implement Logic for changing keybinds and finally we will go through saving and loading our keybinds the UI asset I am using in this game is sci-fi GUI Pro kit this asset contains all components required to make beautiful menus for your sci-fi game the link for sci-fi G UI Pro kit is available in the description let's get started in your project go to package manager from the filter select Unity registry next scroll down to input system import it into your project Unity will most probably restart after importing the input system again go to package manager and then to input manager under samples of input manager look for rebinding UI and import it now let's get started with setting up our scene I will create a simple scene with a cube is ground for the player I will use a simple capsule with character controller next let's add player input I'm a player object add the player input component here click on create actions save your new input actions the window that opens up shows all the default actions that Unity automatically creates these include using keyboard to move Mouse to look and mouse button to fire you can change these as required and also add your own actions I will add jump action and set it to spacebar key once done close the actions window on the player object in player input component at the bottom we can see Behavior the drop down will show all the possible methods for sending the input data to our player change this to Unity events and now we should see all the actions with which we can attach Unity functions to call let's quickly set up a player control script and write the functions which we will attach to player input component the script will simply move the player based on value of move Direction and if the player is not grounded it will apply gravity we will need two functions which we will attach to the player input one for moving and one for jumping the parameters for these functions are going to be input action callback context for this we will need to import the input system Library and now we can simply read the vector 2 value from the context and set our move Direction variable in the jump function instead of reading the value we will check if the action is performed if it is we will make the player Jump by adding velocity in the upwards Direction back to Unity attach the move and jump functions to the player input component and run the project to test the script next we are going to make our rebind menu a background image with a title and two buttons one for ok and one to reset all key binds to default values next go to samples input system version number rebinding UI here drag and drop the rebind UI prefab inside your menu the prefab has three components a label to show input name a button in the middle which shows current map key pressing this button will start rebinding and another button to reset to default now let's bind this prefab to a single input action select the prefab and inside action drag and drop the action you want to set up rebinding for you can select the action from the input actions we saved previously I will set up rebinding for the move action from binding drop down I will select up which is bound to W key on keyboard next let's change the visuals of The Binding prefab to match our UI since I will be using my own labels and already have a reset button I will disable the label and the reset button a matching background for the button and a matching font style and on the side a label for name of the action now we can just duplicate the prefab for every key we want to rebind adjust the UI accordingly I will create two groups in my UI one for movement keys and one for actions keys next simply assign the action to the rebind script for each prefab change the labels accordingly next let's start adding new code to make everything work create a new script on the rebinding menu in the script the first thing we need to do is disable all the actions when the rebuy menu activates and re-enable them when the menu deactivates if we don't disable actions while rebinding we will get a runtime error in unity assign the actions to this new script if we try to run our project we will get an error this is because we require an overlay when rebinding starts you can make this overlay a pop-up which is requesting the user to press a key or as I'm going to do it make the overlay transparent and make it cover the whole screen this way the user will not be able to click on anything in the menus while rebinding is in progress make sure that the overlay is disabled by default assign this overlay to all the keybind prefabs next let's modify the rebinding script to meet our requirements go to the location of rebind UI prefab here you should see a script called rebind action UI open it up in the script go to the function called perform Interactive rebind here go to the part where the script is changing the rebind text the script is currently trying to display a message on the overlay asking the user to press a key to rebind the selected action instead what we want to do is change the text of the label where the current map key is displayed to something like press any key comment out the part we don't need and set the binding text which displays the current map key to the message we want to display let's run the project and perfect we can rebind our keys let's add another script for pausing the game so we can display the rebinding menu and then unpause it when we are done we will Pause by simply setting time scale to zero and enabling our menu and do the opposite for unpausing the pause function will be called from player input so we will need the input action callback context and we will need to check if the Callback context action is performed in the player controls we saved earlier add the pause action which will be assigned to the Escape key I'm a player input component assign the function for the pause action disable the rebinding menu on Star and test the project perfect we can pause our game change key binds unpause the game and play the game with our new keys next let's set up the reset and OK buttons in the reset button we are simply going to call reset to default function of all key bind prefabs for the OK button we will create a new function to unpause the game attach the unpause function to OK button and all done as we can see everything is working perfectly the last thing we are going to do is save our key binds so they don't reset every time we restart our game the script for this is already available in samples by default the script saves the values on disable and loads them on enable you can change this based on your game logic however I am simply going to add this script to my rebinding menu without any modifications this way when game is paused the values will be loaded and they will automatically be saved when the menu is closed you will need to make sure that the load function runs when the game starts for this simply copy it to any script which runs on Gamestar and all done our rebinding logic is now running perfectly and that's it for how to make a key binding menu in unity if you found this video useful make sure to like And subscribe
Info
Channel: The Unity Dude
Views: 5,975
Rating: undefined out of 5
Keywords: unity, unity3d, game dev, unity keymapping, unity 3d, unity engine, game development, unity rebinding, unity key mapping, unity key binding, unity keybinding, runtime keymapping, runtime key mapping, runtime rebinding, runtime key binding
Id: WAKXn2SKJDU
Channel Id: undefined
Length: 12min 10sec (730 seconds)
Published: Mon Aug 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.