Button Controllers

While Unity's UI provides a lot of very useful features, it usually requires some scripting to make the most of it. To reduce the amount of coding you have to do, a button controller system has been provided that is used throughout the Vehicle Combat Kits, and you can use it too when building your own menus.

The two key parts of this system are the Buttons List Controller and the Button Controller components.

Creating a Button List

To create a list of buttons, add a Buttons List Controller to a gameobject in your scene.

In the inspector, you can set the button controller prefab (see the next section), as well as the audio that the buttons should use. You can also set the parent for the buttons when they are created (which will usually be a Horizontal or Vertical Layout Group).

Creating a Button

To create a new button:

  1. Create the button with Unity's UI and add a Button Controller component to it

  2. Customize the inspector settings

  3. Drag it into the Project view to create a prefab

  4. Set the prefab as the Button Prefab in the inspector of the Buttons List Controller you set up in the previous section

In the inspector of the Buttons List Controller component, you have a huge range of options for controlling the way that images, icons and texts on the button are shown, their color, and what to show when a button is 'deep selected' - that is, when it is the chosen option but something else was clicked for whatever reason.

Using The Buttons List

The Buttons List Controller is a component that manages a set of buttons in a menu. You can utilize it with the following code:

// Set the number of buttons dynamically
myButtonsListController.SetNumButtons(someNumber);

// Set a specific button as selected
myButtonsListController.SetButtonSelected(someNumber);

You can also add a function to the On Button Clicked event to be notified when a specific button is pressed.

Last updated