How To Create Buttons in Unity | Beginner UI Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to another unity tutorial today and today i'm gonna be showing you how to make a button or a button which will you'll be able to interact with like so and you'll be able to write a simple bit of code to control what the button does when you unclick it and whether that be moved to another level or something very similar [Music] so to get started all you need is a canvas you can right click in the hierarchy choose ui and create canvas if you've already got a canvas i'm just going to get rid of my button i've already got some text at the top which is just and a little background so you can right click your canvas click ui and choose button with text mesh pro and text mesh pro is a little bit better because it's got more functionality for the text that you've got so we've got a button here we can potentially just scale up our button like i had it before you can snap it to the center if you want or wherever your button may go you can drop down the actual button on the left hand side in the hierarchy and select the text mesh pro text and then we can call this whatever we want so in my example it was called new game we can set that as bold and you can set any of the other options so you could say italic underlined whichever you want you can set your own font and we can just up the size and i'll just drag the size like so and put it like this you can leave the actual button with a background and you can change the text color if you really want you can do other customizations with the text for adding outlines underlays and all fancy things like so we can click back on our button and you have the settings on your button here you have the image which is the actual button image itself you have the whether it's interactable and you have the actual selection of colors that you can have when you're highlighting or something else so the normal color is as it looks like now so we can change that if we wanted to you have the highlighted color that when you select over it it will change you the pressed color that when you click it down the disabled color when you can interact with it at all and then you have the on click event which lets you do something when you click the actual button and i just want to quickly mention to everybody that you can get this project all the script and and you can get every other thing that i featured in my tutorials on my patreon what we can do is we could set the highlighted color to a red and we can press play in this case and you can see like so we need to add a little simple script so we could add this script either onto our button or somewhere else i'm just going to create an empty game object and then from my empty game object i'm just going to call this button controller and then on that button control i'm going to add a new component add a new script and we're going to call this button ui press enter and then we're going to open up in visual studio and then what we're going to do is to reference a button to be able to do something in this case we're going to just load a scene so we'll say using using unity engine dot scene management and then what we can do is we can write a public method which is going to be how we access something through the button so we'll say public void and we can call this new game button and have two brackets then two curly brackets below and say scene manager dot load scene and in brackets we can tell it the scene all the index that we want to load so at the top above we can create a variable which is called private string new game level so that equal to level one or whatever for just by default that it's going to be and we can put paste that in here put a semicolon and we are ready to go we can go back into unity now we can select on the button controller you can see level one is there you need to make sure that you have a scene called level one or something similar whichever that's going to be you need to make sure you go file build settings and add a different level to this so what i'll do is i'll make just a new copy of this level by saving as and i'll go into scenes and i'll just call this level one i'll add level one to the build i'm on level one right now and to make sure that i know this is level one i will click on my text box and i'm just gonna call this one level one and we'll go back to the other scene which was my ui button we'll go back to our button we'll add the on click event here we'll add our button controller to this slot we'll go to the function choose the actual function choose the script that we want then choose the new game button then make sure that in your button controller you have level one you press play you will get the same scenario click and you will see that we went to level one so this was just the basis of creating a simple button be able to use the on click event to change the scene that we've got but you can also do anything you want on that button press but that's as simple as it needs to be so thank you very much for watching take a look at my patreon if you want to get access to all the scripts projects and so much more so come and join me on discord check out my great assets on the unity store and thanks very much for watching and don't forget to like comment subscribe cheers
Info
Channel: SpeedTutor
Views: 81,876
Rating: undefined out of 5
Keywords: unity3d, unity3D, unity3d tutorials, unity tutorial, create a button in unity, unity beginner tutorial, create a button unity, button unity, button click sound effect unity, unity button press, unity UI tutorial, how to button unity, animated button in unity, how to make a button in unity 3d, how to make a button in unity c#, unity tutorials, beginner tutorial unity, button design unity, unity menu button tutorial, changing level on button press unity, speedtutor, tarodev, sykoo
Id: gSfdCke3684
Channel Id: undefined
Length: 5min 38sec (338 seconds)
Published: Thu Jul 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.