Class Button
This is a clickable button.
Inherited Members
Namespace: UnityEngine.UIElements
Syntax
public class Button : TextElement, IEventHandler, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IResolvedStyle, IBindable, ITextElement, INotifyValueChanged<string>
Remarks
A Button consists of a text label element that can respond to pointer and mouse events. You can replace or add to the content of the button by adding elements to its hierarchy. For example, to use a separate image as an icon for the button, you can make an Image element a child of the button.
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.
Constructors
Button()
Constructs a Button.
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
ussClassName
USS class name of elements of this type.
Declaration
public static readonly string ussClassName
Field Value
Type | Description |
---|---|
String |
Remarks
Unity adds this USS class to every instance of the Button element. Any styling applied to this class affects every button located beside, or below the stylesheet in the visual tree.
Properties
clickable
Clickable MouseManipulator for this Button.
Declaration
public Clickable clickable { get; set; }
Property Value
Type | Description |
---|---|
Clickable |
Remarks
The default Clickable object provides a list of actions that are called using one or more activation filters.
To add or remove activation triggers, modify
clickable.activators.Add(new ManipulatorActivationFilter(...))
or
clickable.activators.Clear()
Methods
DoMeasure(Single, VisualElement.MeasureMode, Single, VisualElement.MeasureMode)
Declaration
protected override Vector2 DoMeasure(float desiredWidth, VisualElement.MeasureMode widthMode, float desiredHeight, VisualElement.MeasureMode heightMode)
Parameters
Type | Name | Description |
---|---|---|
Single | desiredWidth | |
VisualElement.MeasureMode | widthMode | |
Single | desiredHeight | |
VisualElement.MeasureMode | heightMode |
Returns
Type | Description |
---|---|
Vector2 |
Overrides
Events
clicked
Callback triggered when the button is clicked.
Declaration
public event Action clicked
Event Type
Type | Description |
---|---|
Action |
Remarks
This is a shortcut for modifying clicked. It is provided as a convenience. When you add or remove actions from clicked, it adds or removes them from Clickable.clicked
automatically.
onClick
Obsolete. Use clicked instead.
Declaration
public event Action onClick
Event Type
Type | Description |
---|---|
Action |