How to use button in Unity

LoadScene will take place immediately, so it need to delay a period of time enough to here the sound effect.

We just create 2 public variable AudioSource in code, but we need to "explain" for Unity can understand "what are they".

Drag and dropIn previous tutorials, gamemakerlab had guided you how to create image and sound in game, switch bettween Scenes (from Scene SplashScreen to MainMenu ).

In this tutorial, gamemakerlab will guide you how to create a simple menu with buttons.

Our lesson will guide you how to:

Adding code to button

Changing color effect for button

Function has variable for button


Step 1: Create Button

Choose Scene MainMenu, in the Hierarchy area -> Create -> UI -> Button.

A button will appear in the cavas area like the below picture. the corresponding component AudioSource in each position like the below picture

how to use button in unity

Normally, a button is created having three parts.

  • Component Image: show image for button.
  • Component Button: control for button.
  • Text: text in button, this part is a "child" game Object. it's below the GameObject button which is just created (parent). 

Step 2: Adjust the shape of the button

We can adjust the shape of the button created as what we want by change Source Image in Component Image and the text content by the method which is mentioned in the first tutorial.

how to use button in unity

Step 3: Using the Button

When press the Play button, Game will switch from Scene MainMenu to Scene PlayGame.

Create a Empty GameObject to control buttons, name it as MainMenuManager.

Create a c# code Script MainMenuManager and add to this GameObject.

how to use button in unity

Open that c# file and add the below code:

how to use button in unity

Next, we will add the BtnPlayOnClick() function and the Play button.

To do that we will use the available support in component Button.

Back to Unity, click on the "+" symbol in OnClick()

how to use button in unity

Choose MainMenuManager.

how to use button in unity

In line No Function, select MainMenuManager -> BtnPlayOnClick()

how to use button in unity

We need to create Scene 2_PlayGame and add in the Scenes In Build of Build Setting.

Step 4: Create effect for button:

  • In the component Button, Unity has available support of changing color:
  • Normal Color: color in the normal state.
  • Highlighted Color: color when moving mouse into button.
  • Pressed Color: color when click in button.

Along with color, if you pay attention, you can see under the color have a white-black small bar, it shows alpha parameter for the corresponding colors. You can adjust the parameters to create the effect of blurred or darker than the button.

how to use button in unity

Step 5: Add function have parameters into button

In some case (like using a function for many buttons), we will set a parameter to distinguish, this parameter will appear in Unity like the two below images. 

how to use button in unity

how to use button in unity

Note: Unity only support function with no more than one parameter, if we use one function have multi parameters, this one won't appear in the choose function of OnClick ().