Class Button
A button triggers an action or event when pressed.
Inheritance
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class Button : ExVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder, ISizeableElement, IPressable
Remarks
Buttons are interactive elements that users can click or tap to trigger actions. They serve as key interaction points in the user interface, guiding users through their journey and enabling them to accomplish tasks.
The Button component provides several customization options including different variants, sizes, and the ability to include icons and text labels. This flexibility allows you to create buttons that match your application's design language while maintaining consistency and usability.
Use buttons for the most important actions in your interface. For less important or secondary actions, consider using alternative components like links or menu items.
Variants
- Default: Standard button style for most use cases
- Accent: Highlights the primary action in a group of buttons
- Destructive: Indicates actions that may have destructive consequences
Examples
Here are some common button usage patterns: various button configurations for different use cases.
// Basic button with title
<Button title="Click Me" />
// Primary action button
<Button variant="Accent" title="Save Changes" />
// Destructive action with warning
<Button
variant="Destructive"
title="Delete Item"
subtitle="This cannot be undone"
leadingIcon="trash" />
// Icon-only button
<Button leadingIcon="settings" size="S" />
// Quiet secondary action
<Button quiet="true" title="Cancel" />
Creating and handling buttons in C#: programming button behavior.
// Create a new button
var saveButton = new Button();
saveButton.variant = ButtonVariant.Accent;
saveButton.title = "Save Changes";
saveButton.leadingIcon = "save";
// Handle click events
saveButton.clicked += () => {
SaveChanges();
Debug.Log("Changes saved!");
};
Constructors
Button()
Default constructor.
Declaration
public Button()
Button(Action)
Constructs a button with an Action that is triggered when the button is clicked.
Declaration
public Button(Action clickEvent)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | clickEvent | The action triggered when the button is clicked. |
Remarks
By default, a single left mouse click triggers the Action. To change the activator, modify clickable.
Fields
iconOnlyUssClassName
The Button icon only variant styling class.
Declaration
public const string iconOnlyUssClassName = "appui-button--icon-only"
Field Value
| Type | Description |
|---|---|
| string |
leadingContainerUssClassName
The Button leading container styling class.
Declaration
public const string leadingContainerUssClassName = "appui-button__leadingcontainer"
Field Value
| Type | Description |
|---|---|
| string |
leadingIconUssClassName
The Button leading icon styling class.
Declaration
public const string leadingIconUssClassName = "appui-button__leadingicon"
Field Value
| Type | Description |
|---|---|
| string |
quietUssClassName
The Button quiet mode styling class.
Declaration
public const string quietUssClassName = "appui-button--quiet"
Field Value
| Type | Description |
|---|---|
| string |
sizeUssClassName
The Button size styling class.
Declaration
public const string sizeUssClassName = "appui-button--size-"
Field Value
| Type | Description |
|---|---|
| string |
subtitleUssClassName
The Button subtitle styling class.
Declaration
public const string subtitleUssClassName = "appui-button__subtitle"
Field Value
| Type | Description |
|---|---|
| string |
titleContainerUssClassName
The Button title container styling class.
Declaration
public const string titleContainerUssClassName = "appui-button__titlecontainer"
Field Value
| Type | Description |
|---|---|
| string |
titleUssClassName
The Button title styling class.
Declaration
public const string titleUssClassName = "appui-button__title"
Field Value
| Type | Description |
|---|---|
| string |
trailingContainerUssClassName
The Button trailing container styling class.
Declaration
public const string trailingContainerUssClassName = "appui-button__trailingcontainer"
Field Value
| Type | Description |
|---|---|
| string |
trailingIconUssClassName
The Button trailing icon styling class.
Declaration
public const string trailingIconUssClassName = "appui-button__trailingicon"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The Button main styling class.
Declaration
public const string ussClassName = "appui-button"
Field Value
| Type | Description |
|---|---|
| string |
variantUssClassName
The Button variant styling class.
Declaration
public const string variantUssClassName = "appui-button--"
Field Value
| Type | Description |
|---|---|
| string |
Properties
clickable
Clickable Manipulator for this Button.
Declaration
[CreateProperty]
public Pressable clickable { get; }
Property Value
| Type | Description |
|---|---|
| Pressable |
contentContainer
The content container of this element.
Declaration
public override VisualElement contentContainer { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
Overrides
leadingIcon
The Button leading icon.
Declaration
[CreateProperty]
[UxmlAttribute]
public string leadingIcon { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
quiet
The quiet state of the Button.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool quiet { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
size
The Button size.
Declaration
[CreateProperty]
[UxmlAttribute]
public Size size { get; set; }
Property Value
| Type | Description |
|---|---|
| Size |
subtitle
The subtitle of the Button.
Declaration
[CreateProperty]
[UxmlAttribute]
public string subtitle { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
title
The title of the Button.
Declaration
[CreateProperty]
[UxmlAttribute]
public string title { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
trailingIcon
The Button trailing icon.
Declaration
[CreateProperty]
[UxmlAttribute]
public string trailingIcon { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
variant
The button variant.
Declaration
[CreateProperty]
[UxmlAttribute]
[Header("Button")]
public ButtonVariant variant { get; set; }
Property Value
| Type | Description |
|---|---|
| ButtonVariant |
Methods
GetSizeUssClassName(Size)
Declaration
public static string GetSizeUssClassName(Size enumValue)
Parameters
| Type | Name | Description |
|---|---|---|
| Size | enumValue |
Returns
| Type | Description |
|---|---|
| string |
GetVariantUssClassName(ButtonVariant)
Declaration
public static string GetVariantUssClassName(ButtonVariant enumValue)
Parameters
| Type | Name | Description |
|---|---|---|
| ButtonVariant | enumValue |
Returns
| Type | Description |
|---|---|
| string |
Events
clicked
Event triggered when the Button has been clicked.
Declaration
public event Action clicked
Event Type
| Type | Description |
|---|---|
| Action |