A ClickEvent occurs when the user clicks the left mouse button (or the first button on a pointing device) over a VisualElement.
A click consists of a pointer down event followed by a pointer up event on the same VisualElement. The pointer is allowed to move between the two events, as long as the down and up events occur over the same VisualElement.
This event can be used to detect clicks on visual elementsA node of a visual tree that instantiates or derives from the C# VisualElement
class. You can style the look, define the behaviour, and display it on screen as part of the UI. More info
See in Glossary that aren’t buttons. For example, the implementation of the Toggle
control uses the ClickEvent
to show or hide the check mark, and to change the control’s value.
The base class for ClickEvent
is PointerEventBase. For more information please also see the documentation on Pointer Events.
Event | Description | Trickles down | Bubbles up | Cancellable |
---|---|---|---|---|
ClickEvent | Occurs when the left mouse button is clicked. | ✔ | ✔ | ✔ |
The ClickEvent
has no unique properties, but inherits all properties from its base class. You can find a list of properties on the Pointer Events page.
Unity sends this event when the left mouse button is clicked over a visual element.
target
: The element underneath the mouse or pointing device when the click occurred.
The following example registers for a ClickEvent
on a visual element:
btnClose.RegisterCallback<ClickEvent, VisualElement>(Clicked, asset); // asset is the root visual element that will be closed
private void Clicked(ClickEvent evt, VisualElement root)
{
root.ShowVisualElement(false);
}
The following example shows how to react to the ClickEvent on a colored visual element. When an element is clicked, its color will change to a new, random color.
To see the example in action, do the following:
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class ClickEventExampleWindow : EditorWindow
{
[MenuItem("Window/UI Toolkit/ClickEventExample")]
public static void ShowExample()
{
var wnd = GetWindow<ClickEventExampleWindow>();
wnd.titleContent = new GUIContent("Click Event Example");
}
public void CreateGUI()
{
// Create a few different colored boxes
for (int i = 0; i < 4; i++)
{
// Create VisualElement with random background color
var newBox = new VisualElement() { style = { flexGrow = 1, backgroundColor = GetRandomColor() } };
rootVisualElement.Add(newBox);
// Register a click event to the visual element to change the background color to a new color
newBox.RegisterCallback<ClickEvent>(OnBoxClicked);
}
}
private void OnBoxClicked(ClickEvent evt)
{
// Only perform this action at the target, not in a parent
if (evt.propagationPhase != PropagationPhase.AtTarget)
return;
// Assign a random new color
var targetBox = evt.target as VisualElement;
targetBox.style.backgroundColor = GetRandomColor();
}
private Color GetRandomColor()
{
return new Color(Random.Range(0, 1f), Random.Range(0, 1f), Random.Range(0, 1f));
}
}
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.