Making a button that changes color
In this recipe, we will see the first basic customization that we can allow the player to do. This customization will allow him to change the color of a button by clicking on it, and then switch between two colors. In this way, you will learn how it is possible to use events to customize buttons when the player interacts with them.
How to do it...
In the first step, we have to create our UI button. Right-click on the Hierarchy panel, then go to UI | Button, and rename it to ChangeColorButton. Of course, it is possible to resize the button, change Source Image, the text, and place it as we wish in Canvas.
Now, it's already time to write our script to handle the whole process. Select ChangeColorButton and, in the Inspector, navigate to Add Component | New Script. Name it ChangeColorButtonScript, and then click on Create and Add.
Double-click on the script in order to edit it. Next, we have to add the
using UnityEngine.UI;
statement at the beginning of the script...