More about UI scripting – handlers

There is one last topic that we need to cover, since we will be using this technique later on in Chapter 7Trading Cupcakes and the Ultimate Battle for the Cake – Gameplay Programming .

Imagine that you want to create an UI component that does something when it is clicked. We could create a script with a public function, then attach a button component to the game object. Finally, we should create a new OnClick() event on the button component to trigger the function we have written before. It's fine, but isn't a bit laborious?

Another example is, suppose you need to drag a UI component around because it is a floating window. How are you going to do it? For what we have seen so far, this appears to be an hard task; but there is an easy solution.

In fact, in our scripts, we can directly include directly the event systems, by using this line of code:

using UnityEngine.EventSystems; 

As a result, you will be able to extend your script with some (C#) interfaces.

Note

In case you don't know what a C# interface is or don't know how to use it, you can check any C# manual. However, I recommend this video from the official Documentation of Unity, since it applies interfaces directly to Unity: https://unity3d.com/learn/tutorials/topics/scripting/interfaces.

These interfaces allow you to create a function within your script that is triggered whenever the specific event you chose occurs. Moreover, this function provides some information about that specific event as a parameter within the PointerEventData class. For instance, to implement our dragging behavior from before, we need to add a handler/interface for the drag event next to the class declaration, like as follows:

public class DragTest : MonoBehaviour, IDragHandler {

Then, you need to implement the interface with its specific function. In this case we have:

    public void OnDrag(PointerEventData eventData) { 
    } 

Tip

In case you are using Visual Studio as your code editor, you can right-click on the name of the interface and from the quick action menu select Implement interface to automatically create that the function we need to implement.

At this stage, implementing the drag behavior is simple, since within the eventData variable, we have all the data regarding the event, such as the mouse position. Therefore, we can write the following line of code:

transform.position = eventData.position; 

Note

Alternatively, you can also use the Input class, as shown here: transform.position = Input.mousePosition;

For a complete list of events you can consult the official documentation here: https://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html.

For details about the PointerEventData class and which kind of information of an event it holds, here is the link to the official documentation: https://docs.unity3d.com/ScriptReference/EventSystems.PointerEventData.html.

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

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