Buttons
Add buttons to your Unity Spark UI.
Read time 2 minutesLast updated 3 hours ago
A button is an element that the player selects to start an action, such as Play, Pause, or Restart. Players click it with a mouse, or tap it on a touch screen.
Add buttons
To add a button to your UI:
- Select a UI element in the Hierarchy window to open the UI Editor.
- Select Button in the UI Editor's lower toolbar.
- Click the canvas to place the button, or drag on the canvas to draw it at the size you want.
Alternatively, in the Hierarchy window, select Add object (+) > UI > Button.
Change button labels and roles
A new button is a shape with the Primary Button style role, and a Text element inside it.
To change the label:
- Select the text inside the button.
- Change its Text.
For a less prominent button, such as Cancel, change the button's Role to Secondary Button.
Button settings
The Button component contains the following settings.
Setting | Description |
|---|---|
| Target Graphic | Sets the part of the button that changes color when the player hovers over it or selects it. |
To make any shape behave like a button, add a Button component to it.
Buttons, sliders, toggles, input fields, and scrollbars share the following settings.
Setting | Description |
|---|---|
| Interactable | Sets whether the player can use the control. When Interactable is disabled, the control shows its Disabled Color. |
| Colors | Sets the tint for each state: Normal Color, Highlighted Color (when the pointer is over it), Pressed Color, Selected Color, and Disabled Color. Color Multiplier brightens every state, and Fade Duration sets how long a change takes. |
Respond to button clicks
A button does something only when your game responds to it. The Inspector window doesn't have a list of click actions, so use the AI assistant or a script.
To use the assistant, describe what the button does, for example, For more examples, refer to Add button behaviors and create screens with the assistant.
When the player clicks Restart, reload the current scene.To respond to a click in a script, pass a function to the button's .
OnClicklocal function onRestart() -- what happens when the player clicks the buttonendrestartButton:OnClick(onRestart)
Pass a function that you define in the same script. Don't pass , because the event calls the function you give it.
self