How to use Text, Input, & Buttons in your Game - UNITY 101

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey game devs in this video I'm going to cover text input and buttons if you're already really familiar with those go ahead and just hit the thumbs up button and continue but if you're trying to figure out how to add text how to read inputs or how to use buttons in your game just give me about four minutes and by the end of it it should all make sense to add text to your game you'll go to game object UI and then choose text Dash text mesh Pro I can zoom out to see the text on my full canvas use the middle Mouse button to move around and I can drag this text object all around or even resize it scale it to fit my entire screen change out the text value and crank up the font size so that it's nice and big whatever I want to do I can modify right in here but if I want to change things at runtime I need to write some code so let's see how we can write code to update this text while we're playing let's take a look at this simple text updater script it has the single job of updating our text value to match the number of seconds our game has been playing whenever our text object is enabled and if I try that in the editor let's expand out the text mesh Pro text and press play you'll see that my number of seconds will show up right here in my text and that'll be the value there if I disable and it re-enable my text I get the new value for how many seconds we've been running our game for now let's add a button to refresh that time whenever it's clicked to add a button we go to game object UI and choose button text mesh Pro I want my button to be big so I'm going to dock it to the top right corner and set the height to 300 and the width to 300 as well then I'm going to change the text by expanding out the button and selecting the text text mesh Pro object underneath it I'll make this a refresh and then I'll scale it up using the font size auto option and crank up the max value there looks like it's getting to about 85 and that seems good if I press play right now my button will look like it's clickable I can click on it and make it change visually but it won't actually do anything so I need to stop playing and then go select my button object if I scroll down to the button component and make sure it's expanded and open you should see an on click section and it should have a nice empty list here and this is where we're going to tell it what we want it to do when it's clicked to do that we're going to have to hit the plus button and it's going to ask us for the object that we want to run code on we want to run code on this player's name text right here so I'm going to drag this text object over here and click on the function objects this will show me all of the components on that object including my text updater and all of the public methods on there now my on enable method isn't public and I really don't want to call an on enable method from a button instead what I want to do is go back into that code and make a quick refactor by moving the time text code into a public method named refresh time text and just calling that an on enable I can now add that as an on click listener so I choose my drop down choose text updater and I can find refresh time text right here now when I press play okay I'll be able to click the refresh button and call the code to update my UI element whenever I want you can see every time I click it updates you can add more than one listener to your buttons as well so that multiple things happen in this example I've just made it so that my refresh text will set to inactive as soon as I click not very helpful but you get the idea when you're using buttons though it's also important to know that you don't have to add your listeners in the inspector another very common way to do this is using the add listener method here I've changed the text object you call add listener on the button and the button is just assigned to the text updater this will do the same exact thing just make sure that you call remove listener so that you don't accidentally double add listeners and have a method get called twice now let's take a look at input let's say we want our player to put their name in that can't be too hard right we'll go to game object choose UI and again choose input field text mesh Pro the first thing I'll do is resize this so it's a lot easier to see I'll choose anchor and we'll scale and stretch it along the bottom set the site to or the height to about 100 and then we're going to scroll down expand out the children here and we need to select both the placeholder and the text this is the placeholder text there that says enter text and the text when we start typing we're going to choose Auto size on both of them and just make them go up to about a 72 so that I can see them to read that input field I can just add a reference in my script and then in my refresh time text that should probably be renamed I can set the text to the value by reading the dot text property let's give it a try put in my name here is subscribe and hit refresh there it is make sure you subscribe let me know if you have comments or questions and I'll see you in the next one
Info
Channel: Jason Weimann
Views: 4,832
Rating: undefined out of 5
Keywords: unity3d, unity Text, unity input, unity button, textmeshpro, text mesh pro, unity text input, unity, unity textmeshpro, unity tutorial, unity textmesh pro set text, unity textmesh, how to use text mesh pro, unity3d college, text, unity game tutorial, textmesh, unity ui, textmesh pro, unity 2d text, unity tutorial for beginners, unity 2d tutorial, unity ui tutorial
Id: DeT4L9e6syw
Channel Id: undefined
Length: 4min 48sec (288 seconds)
Published: Wed Aug 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.