# UI Events

To understand more about the UI system, here is a simple demonstration of how to create a working button.&#x20;

Step 1 :&#x20;

Create an empty scene with any name of your liking and open the scene.

Step 2 : Adding necessary scene game objects for this exercise.

Go to the Prefabs folder inside path UIs/Essential and drag ChessGameObjectBase prefab into the scene hierachy.

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2Fkd3wvGbSdk1Zod4euBvf%2FUnity_vlZN32M8ui.png?alt=media&#x26;token=5666c07e-3405-4ae9-81fc-03ab2be728d1" alt=""><figcaption></figcaption></figure>

Then, create a canvas on the scene and go to another folder inside Prefabs folder UIs/Buttons/Base, drag the ResponsiveButton prefab to the screen canvas.

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FU4SL0EJY7OmUy56mEqBQ%2FUnity_rU8duD6USk.png?alt=media&#x26;token=24a7baff-20db-4e22-b68f-a0606f517ba9" alt=""><figcaption></figcaption></figure>

Scene result as below image

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FTlf3hHx1u6469GSlQtG6%2FUnity_YZfZGDHSjS.png?alt=media&#x26;token=96228003-c69b-42ce-b661-c7dac0d4cd05" alt=""><figcaption></figcaption></figure>

Step 3 :&#x20;

Copy code below to ChessEventManager.cs located in Scripts/Main/Communicators folder, the string parameter is optional, it's just there to showcase the possibilities of passing parameters values.

```
    public delegate void OnButtonDelegate(string text);
    public static event OnButtonDelegate OnButtonEvent;

    public static void InvokeButtonEvent(string text)
    {
        OnButtonEvent?.Invoke(text);
    }
```

Step 4 :&#x20;

Create a C# script in the path Scripts/Main/UIs/Buttons name DummyButton.cs and copy the code below into it. As soon as the button is clicked, it will invoke the OnButtonEvent event, nothing will happen for now since nothing is listening to this event.

<pre><code>namespace TheChess
{
<strong>    public class DummyButton : ResponsiveButton
</strong>    {
        protected override void StartListening()
        {
            btn.onClick.AddListener(delegate
            {
                ChessEventManager.InvokeButtonEvent("Dummy button clicked");
            });
        }
    }
}
</code></pre>

Step 5 :&#x20;

Remove the ResponsiveButton(Script) component from the scene gameobject and add the newly created DummyButton script to it. Drag the text gameobject to the objectfield.

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2F18LiMULGWdFEdevcaHzS%2FUnity_CXnAJrLq4I.png?alt=media&#x26;token=1a620958-e817-4bf4-8734-c9bfb1697c9e" alt=""><figcaption></figcaption></figure>

Step 6 : This is where when button clicked, ChessBaseGUI class will respond.

Open ChessBaseGUI script file in Scripts/Main/GUIs folder and copy below function into it

```
    void DummyButtonCall(string text)
    {
        Debug.Log(text);
    }
```

Then add below line of code into `SubscribeChessEvents()` function, it means ChessBaseGUI will start listening to when button invoke OnButtonEvent, DummyButtonCall function will get called.&#x20;

```
    ChessEventManager.OnButtonEvent += DummyButtonCall;
```

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2F3ELrwhA671ylAbcjENkN%2Fdevenv_3dUDYObf2f.png?alt=media&#x26;token=1db89ad3-bc96-40e6-872d-76fc9c82c7c0" alt=""><figcaption></figcaption></figure>

It is also important to add a same line of code as previous line but with -= operator to `UnsubscribeChessEvents()` function, to ensure ChessBaseGUI stop listening when exiting scene.

```
        ChessEventManager.OnButtonEvent -= DummyButtonCall;
```

Step 7 :&#x20;

Play and click the button

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FfGLryZWBSXmYaya2G7ES%2FUnity_mPDhMH6nh4.png?alt=media&#x26;token=95af7182-78dd-434c-8529-e22e7e5ac5fb" alt=""><figcaption></figcaption></figure>

:thumbsup:Well done, you can now make the button a variant prefab and drag it to any scene that has gameobject with attached script component ChessBaseGUI or GUIs inherited from ChessBaseGUI and it will work straight away.

Step 8 : Optional

To make the button provide feedback if the click did indeed trigger action. Do the following changes.

Replace the InvokeButtonEvent function in Step 3 to code below

```
    public static void InvokeButtonEvent(string text)
    {
        bool? success = OnButtonEvent?.Invoke(text);

        if (success != null)
            CreateButtonRespond((bool)success);
        else
            SendWarningMessage(typeof(OnButtonDelegate).Name); //This will send warning message if no script is listening to this invoker when it invoked
    }
```

change the code in Step 4 to return bool value

```
    bool DummyButtonCall(string text)
    {
        Debug.Log(text);
        
        return true; //this will determine if it's a tick or cross the button respond with.
    }
```

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FPIpexqoJ4oKZqAkLXeEn%2FUnity_KKXdW8nwMv.png?alt=media&#x26;token=78023665-f2e5-4fd9-8550-78df6ebf3c6b" alt=""><figcaption></figcaption></figure>

Tips : Here is how to find the class that are relevant to the event, quickly and easily.

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FII8CpwdooIBJEHyJtW36%2Ffindref.gif?alt=media&#x26;token=cddf9bb1-6645-4f1c-b7ab-90dc32afa34f" alt=""><figcaption></figcaption></figure>
