Documentation

Unity Spark

Open Unity Spark

Unity Spark

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:
  1. Select a UI element in the Hierarchy window to open the UI Editor.
  2. Select Button in the UI Editor's lower toolbar.
  3. 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:
  1. Select the text inside the button.
  2. 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 GraphicSets 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

InteractableSets whether the player can use the control. When Interactable is disabled, the control shows its Disabled Color.
ColorsSets 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,
When the player clicks Restart, reload the current scene.
For more examples, refer to Add button behaviors and create screens with the assistant.
To respond to a click in a script, pass a function to the button's
OnClick
.
local 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
self
, because the event calls the function you give it.

Additional resources