Buttons

One of the most common UI elements is a simple button. Pretty much all UI interfaces on the most commonly available Unity platforms contain buttons. The simplest interaction with a button is a click. You will now learn how to construct a button in Unity UI and call a certain method in the code when the button is clicked on.

To speed up your learning process, I have prepared ready-to-use UI elements.

Download and import MenuViewUIElements.unitypackage into your Unity project, as follows:

Buttons

Unity will import a few useful assets with this package. In the Project View, find Prefabs/UI/PlayButton.prefab and drag it directly on top of the Menu Canvas, as shown here:

Buttons

When you drop the prefab, the PLAY button should appear on the canvas, like this:

Buttons

A simple button

Before we talk about the functionality of the button, we will talk about the visual parts that make up the simple labeled button:

A simple button

Image

Unity can very easily draw 2D images on the UI canvas. Select your button and take a look at the Inspector window. We are using the Image component on our button to give it a visual presence. In simple words, the Image component gives our button a background.

The Button component

The actual interaction with the button is controlled by the Button component. The Button control responds to a click from the user and is used to initiate or confirm an action. For more detailed information, refer to the Unity Manual. You will find tons of useful information and examples in it: http://docs.unity3d.com/Manual/script-Button.html.

Interaction

There it is! This is our first button. It might not look very impressive, but it's functional. To check how it works, click on Play in Unity and press the button in the game view.

What should happen is… nothing. You observe the button visually changing a little when you press it, but the game doesn't start. We need to assign an action to the button to call the part of code that we need.

All user interaction with Unity UI is driven by events. The button has one type of event already built into its component. It is the simple OnClick event:

Interaction

The OnClick event is invoked when a user clicks on the button and releases it. At the moment, the list of methods that we want to invoke when the OnClick event happens is empty. Hey! That's why the button isn't doing anything useful. Let's hook it up with a method and see how it works.

The Button action

Unity's Event Systems are very flexible. The OnClick event allows you to call any public method in your code directly from the Button component. Follow the few simple steps given here to assign a specific method in the code that you want to call when the UI button is clicked on by the user:

  1. Select the game object containing the Button component.
  2. Press the + button in the Event section, as shown here:
    The Button action
  3. Drag and drop the GameObject containing the script with the public method you want to call from the event. In this case, it is the GameManager game object. Drop GameManager into the Object field:
    The Button action
  4. From the function list, select the Component name and then the method name of the function that you want to be called when the button is clicked on. Since we are playing with the Play button, we will call the StartGame() function from GameManager , as shown next:
    The Button action

    Note

    If you cannot see your function on the function list, it probably means that EventSystem cannot access the function as it is private. Change the access modifier to public.

  5. If you have connected everything properly, the OnClick() event attached to the Play button will look like this in the Inspector window:
    The Button action

We now have all the bits and pieces we need for the button to work. We have set up the Canvas view to render UI elements. We have an EventSystem to process and trigger events caused by the user. And we also have the first event called when the user presses the Play button.

Great work so far. Let's test the Play button! Press Play in Unity and click on the green Play button in the Game view.

Woohoo! It's working! Great work! As soon as you press the Play button, the Event manager will invoke the StartGame() function, which is under GameManager, and the game will start.

The button is working great. However, something is not right. The user will expect the menu to disappear just after they've pressed the button. Let's get this sorted right away.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset