Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Button

Represents an interactive UI button element.
Read time 10 minutesLast updated 5 days ago

Definition

[UxmlElement(libraryPath = "Controls")][Icon("UIToolkit/Icons/Button.png")]public class Button : TextElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IVisualElementScheduler, ICustomStyle, IBindable, INotifyValueChanged<string>, ITextEdition, ITextElementExperimentalFeatures, IExperimentalFeatures, ITextSelection

Remarks

A Button has a text label element that can respond to pointer and mouse events. You can add an icon to pair it with the text by assigning a Background (Texture, RenderTexture, Sprite or Vector Image) to the iconImage API or icon-image UXML property. Please note that by providing an icon image, this will automatically update the Button's hierarchy to contain an Image and a text label element.
By default, a single left mouse click activates the Button's Clickable property button. To remove this activator, or add more activators, modify the
clickable.activators
property. For details, see ManipulatorActivationFilter.
To bind a Button's text value to the contents of a variable, set the
binding-path
property in the UXML file, or the
bindingPath
property in the C# code, to a string that contains the variable name.
For more information, refer to UXML element Button.
The following is a simple example of how to use a button. Using the clicked event to print a message to the console when the button is clicked.

Examples

using UnityEngine;using UnityEditor;using UnityEngine.UIElements;public class ButtonExample : EditorWindow{ [MenuItem("Window/Button Example")] public static void ShowExample() { GetWindow<ButtonExample>(); } void CreateGUI() { var button = new Button { text = "Click me" }; button.clicked += OnClick; rootVisualElement.Add(button); } void OnClick() { Debug.Log("Clicked!"); }}

Static Fields

Value

Description

iconOnlyUssClassNameThe USS class name for Button elements with an icon only, no text.
iconUssClassNameThe USS class name for Button elements with an icon.
imageUSSClassNameThe USS class name of the image element that will be used to display the icon texture.
ussClassNameUSS class name of elements of this type.

Constructors

Constructor

Description

Button()Constructs a Button.
Button(System.Action)Constructs a button with an Action that is triggered when the button is clicked.
Button(UnityEngine.UIElements.Background,System.Action)Constructs a button with a Background and an Action. The image definition will be used to represent an icon while the Action is triggered when the button is clicked.

Properties

Property

Description

clickableClickable MouseManipulator for this Button.
iconImageThe Texture, Sprite, or VectorImage that will represent an icon within a Button element.

Events

Member

Description

clickedCallback triggered when the button is clicked.

Inheritance

Inherited Members