To create a custom control, you must create a custom control C# class and initialize it. You can expose it for use in UXML and UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary Builder. You can also bind it to data.
Create a C# class derived from the VisualElement
class or a subclass of the VisualElement
class. For example, you can create a bindable custom control derived from the BaseField
generic base class instead of BindableElement
. This provides the following advantages:
INotifyValueChanged
interface for the generic parameter type that you specifyBaseField
.Note: It’s possible to create custom controls derived from built-in UI controls, if you understand their internal hierarchy and existing USS classes. Unity discourages this practice because your custom controls might be dependent on their internal structure, which is subject to change in the future.
Custom controls inherit from VisualElement
. A VisualElement
isn’t bound to the lifetime of a GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary
and doesn’t receive any of these callbacks:
Awake()
OnEnable()
OnDisable()
OnDestroy()
You can initialize a custom control in its constructor. However, if your application needs, you can delay initialization until the custom control is added to the UI. To do this, register a callback for an AttachToPanelEvent
. To detect that your custom control has been removed from the UI, use the DetachFromPanelEvent
callback.
void OnEnable()
{
var myCustomElement = rootVisualElement.Q(className: "my-custom-element");
myCustomElement.RegisterCallback<AttachToPanelEvent>(e =>
{ /* do something here when element is added to UI */ });
myCustomElement.RegisterCallback<DetachFromPanelEvent>(e =>
{ /* do something here when element is removed from UI */ });
}
UI Toolkit dispatches these two events for all elements and doesn’t need a custom VisualElement
subclass. For more information, see Panel events.
To use your custom controls with UXML and UI Builder, you must expose them. To expose custom controls, define a factory class derived from UxmlFactory<T>
.
You can add new attributes to a custom control to use in UXML. To add new attributes to a custom control, define a traits class derived from UxmlTraits
and override the Init()
method as described in Define attributes on elements. The UI Builder relies on this to ensure that the visual elementA 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
created for the ViewportThe user’s visible area of an app on their screen.
See in Glossary updates when you change the value of a UXML property in the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary.
To use your custom controls’ UXML properties in the Inspector of the UI Builder, you must match property names between UXML and C# script. Use standard kebab case notation for UXML, and camel case or Pascal case for C#. For example, a property named MyFloat
or myFloat
in C# should be my-float
in UXML.
You bind custom controls to serialized properties to synchronize values between the control and the property.
To bind custom controls to data:
INotifyValueChanged
interface and listen for ChangeEvent
s as needed.BindableElement
class or implement the IBindable
interface.See SerializedObject data binding for more details.
For a bindable custom control example, see Create a bindable custom control.
Use USS to customize the look of a custom control the same way as a built-in control. You can also create USS custom properties to style a custom control.
Note: The Inspector window in the UI Builder doesn’t show USS custom properties. To edit USS custom properties, use a text editor to edit your USS file directly.
To interact with custom USS properties for a custom control in C#, use the CustomStyleProperty
structure and the CustomStylesResolvedEvent
event.
CustomStyleProperty
describes the name and type of the property you read from the stylesheet.
UI Toolkit dispatches CustomStylesResolvedEvent
for any element that directly receives a custom USS property. It dispatches the event for any element that a selector matches, for selectors for which the rule contains the value of the custom property. UI Toolkit doesn’t dispatch the event for elements that inherit the value. The event holds a reference to an ICustomStyle
object. You must use its TryGetValue()
method to read the effective value of a CustomStyleProperty
. This method has overloads for different types of CustomStyleProperty
.
For a custom style with a custom control example, see Create custom style for a custom control.
Note: You can’t define transitions for custom style properties.
For detailed information on how to handle events for custom controls, see Handle events.
Note:
focus
.To add a custom control to a visual treeAn object graph, made of lightweight nodes, that holds all the elements in a window or panel. It defines every UI you build with the UI Toolkit.
See in Glossary in UI Builder:
Best practices:
EventBase.currentTarget
property.Tips:
Render custom geometry through the generateVisualContent
callback for custom controls. For an example usage that renders a partially filled circle, see RadialProgress example.
Custom controls are convenient. However, you might achieve the same outcomes with the following:
MonoBehaviour
s to add logic that pertains to the specific UI Document that holds your UI. (To learn how to use MonoBehaviour
s to control UI in a UI Document, see Create your first runtime UI.) To achieve encapsulation, create properties and methods inside your MonoBehaviour
that internally fetch VisualElement
s with UQuery and manipulate their properties.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.