A MultiColumnTreeView
is a UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary control commonly used to display tabular or grid-like data with multiple columns. Use a MultiColumnTreeView
to present data in a structured format, where each row represents an item or entry, and each column represents a specific attribute or property of that item.
You can create a MultiColumnTreeView with UXML and C#. The following C# example creates a MultiColumnTreeView with two columns:
var multiColumnTreeView = new MultiColumnTreeView(); multiColumnTreeView.columns.Add(new Column { title = "Name", width = 20 }); multiColumnTreeView.columns.Add(new Column { title = "Power", width = 80 });
To refresh the collection view, in general, call the RefreshItems
or RefreshItem
method. However, in the following cases, call Rebuild
instead to refresh the collection view:
List<float>
to a List<Vector3>
.makeItem
or destroyItem
.
Note: If you call Rebuild
, the collection view is completely rebuilt, which can be expensive. If you call RefreshItems
or RefreshItem
, the collection view is only refreshed, which is less expensive.
You can bind the MultiColumnTreeView
to a data source, and automatically populate the rows and columns based on the provided data. Refer to Create list and tree views for an example.
You can enable sorting for MultiColumnTreeView using a default algorithm, or implementing a custom algorithm if the default sorting operation isn’t fast enough to meet your needs.
Once the sorting is enabled, to sort by a single column, click the column headers. To sort multiple columns, hold down the Ctrl key (macOS: Cmd) and click. To disable sorting, hold down the Shift key and click.
To enable sorting using the default algorithm, set the sortingMode
attribute to ColumnSortingMode.Default
, and implement the Column.comparison
to compare two items by their index in the source.
For an example, refer to a MultiColumnTreeView default sorting example.
To implement custom sorting, set the sortingMode
attribute to ColumnSortingMode.Custom
, and implement the columnSortingChanged
callback. In the callback, use sortColumns
to reorder your list accordingly and trigger a RefreshItems()
once the items’ source has been updated.
The sortColumns
attribute is the current state of sorted columns: it lists the columns in the order in which they’re sorted. If you sort by column 1, then column 2, then column 3, the sortedColumns
list is [1, 2, 3]. If you sort by column 3, then column 2, then column 1, the sortedColumns
list is [3, 2, 1].
Drag-and-drop is a common feature in UI design. To implement the drag-and-drop operations, override the following methods:
canStartDrag
.setupDragAndDrop
.dragAndDropUpdate
. You can perform certain actions based on the drag position or other conditions.handleDrop
.During the drag-and-drop operation, you can enable the reordering of items by dragging. To enable, set the reorderable
attribute to true
in UI Builder, UXML, and C#.
Refer to Create a drag-and-drop list and tree views between windows for an example.
The following are some frequently asked questions about the MultiColumnTreeView control:
Can I get the indices of the rows that are visible on the screen?
There are no dedicated APIs for this. You can use the bindItem
and unbindItem
callbacks to track these indices.
Can I get the list of rows that are visible in the view?
There are no dedicated APIs for this. You can use UQuery to retrieve the elements of interest.
Is it mandatory for any of the overridden functions of a view controller to call base.Method
?
Call this method only if you want to extend its default behavior.
I’ve added a Toggle to my row. Why doesn’t the selection jump to that row when the user selects it?
By default, the row is selected only if the mouse down event is not consumed by the row’s contents. In this case, your Toggle stops the event propagation. To fix this, register a PointerDownEvent
callback with TrickleDown
on the Toggle to call SetSelection
.
How do I receive a callback when a user changes their selection in the view?
It is recommended to use the selectedIndicesChanged
callback to retrieve data by index when needed. While you can also use selectionChanged
, be aware that it returns a list of objects, which can cause boxing allocations when used with value types.
How do I convert from ID to index and vice versa?
Use BaseTreeViewController.GetIndexForId
and BaseTreeViewController.GetIdForIndex
.
Can I have a horizontal MultiColumnTreeView?
The MultiColumnTreeView control doesn’t support horizontal layout and virtualization. It’s recommended to use a ScrollView with flex-direction: row
to layout elements horizontally.
C# class: MultiColumnTreeView
Namespace: UnityEngine.UIElements
Base class: BaseTreeView
This element has the following member attributes:
Name | Type | Description |
---|---|---|
columns |
UIElements.Columns+UxmlSerializedData |
The collection of columns for the multi-column header. |
sort-column-descriptions |
UIElements.SortColumnDescriptions+UxmlSerializedData |
The collection of sorted columns by default. |
sorting-enabled |
boolean |
Whether or not sorting is enabled in the multi-column header. This is deprecated. Use sortingMode instead. |
sorting-mode |
UIElements.ColumnSortingMode |
Indicates how to sort columns. To enable sorting, set it to ColumnSortingMode.Default or ColumnSortingMode.Custom . The Default mode uses the sorting algorithm provided by MultiColumnController , acting on indices. You can also implement your own sorting with the Custom mode, by responding to the columnSortingChanged event.Note: If there is at least one sorted column, reordering is temporarily disabled. |
This element inherits the following attributes from its base class:
Name | Type | Description |
---|---|---|
auto-expand |
boolean |
When true, items are automatically expanded when added to the TreeView. |
binding-path |
string |
Path of the target property to be bound. |
fixed-item-height |
float |
The height of a single item in the list, in pixels. This property must be set when using the virtualizationMethod is set to FixedHeight , for the collection view to function. If set when virtualizationMethod is DynamicHeight , it serves as the default height to help calculate the number of items necessary and the scrollable area, before items are laid out. It should be set to the minimum expected height of an item. |
focusable |
boolean |
True if the element can be focused. |
reorderable |
boolean |
Gets or sets a value that indicates whether the user can drag list items to reorder them. The default value is false which allows the user to drag items to and from other views when you implement canStartDrag , setupDragAndDrop , dragAndDropUpdate , and handleDrop . Set this value to true to allow the user to reorder items in the list. |
selection-type |
UIElements.SelectionType |
Controls the selection type. The default value is SelectionType.Single . When you set the collection view to disable selections, any current selection is cleared. |
show-alternating-row-backgrounds |
UIElements.AlternatingRowBackground |
This property controls whether the background colors of collection view rows alternate. Takes a value from the AlternatingRowBackground enum. |
show-border |
boolean |
Enable this property to display a border around the collection view. If set to true, a border appears around the ScrollView that the collection view uses internally. |
tabindex |
int |
An integer used to sort focusables in the focus ring. Must be greater than or equal to zero. |
virtualization-method |
UIElements.CollectionVirtualizationMethod |
The virtualization method to use for this collection when a scroll bar is visible. Takes a value from the CollectionVirtualizationMethod enum.The default value is FixedHeight . When using fixed height, specify the fixedItemHeight property. Fixed height is more performant but offers less flexibility on content. When using DynamicHeight , the collection will wait for the actual height to be computed. Dynamic height is more flexible but less performant. |
This element also inherits the following attributes from VisualElement
:
Name | Type | Description |
---|---|---|
content-container |
string |
Child elements are added to it, usually this is the same as the element itself. |
data-source |
Object |
Assigns a data source to this VisualElement which overrides any inherited data source. This data source is inherited by all children. |
data-source-path |
string |
Path from the data source to the value. |
data-source-type |
System.Type |
The possible type of data source assignable to this VisualElement. This information is only used by the UI Builder as a hint to provide some completion to the data source path field when the effective data source cannot be specified at design time. |
language-direction |
UIElements.LanguageDirection |
Indicates the directionality of the element’s text. The value will propagate to the element’s children. Setting the languageDirection to RTL adds basic support for right-to-left (RTL) by reversing the text and handling linebreaking and word wrapping appropriately. However, it does not provide comprehensive RTL support, as this would require text shaping, which includes the reordering of characters, and OpenType font feature support. Comprehensive RTL support is planned for future updates, which will involve additional APIs to handle language, script, and font feature specifications. To enhance the RTL functionality of this property, users can explore available third-party plugins in the Unity Asset Store and make use of ITextElementExperimentalFeatures.renderedText
|
name |
string |
The name of this VisualElement. Use this property to write USS selectors that target a specific element. The standard practice is to give an element a unique name. |
picking-mode |
UIElements.PickingMode |
Determines if this element can be pick during mouseEvents or IPanel.Pick queries. |
style |
string |
Sets the VisualElement style values. |
tooltip |
string |
Text to display inside an information box after the user hovers the element for a small amount of time. This is only supported in the Editor UI. |
usage-hints |
UIElements.UsageHints |
A combination of hint values that specify high-level intended usage patterns for the VisualElement . This property can only be set when the VisualElement is not yet part of a Panel . Once part of a Panel , this property becomes effectively read-only, and attempts to change it will throw an exception. The specification of proper UsageHints drives the system to make better decisions on how to process or accelerate certain operations based on the anticipated usage pattern. Note that those hints do not affect behavioral or visual results, but only affect the overall performance of the panel and the elements within. It’s advised to always consider specifying the proper UsageHints , but keep in mind that some UsageHints might be internally ignored under certain conditions (e.g. due to hardware limitations on the target platform). |
view-data-key |
string |
Used for view data persistence, such as tree expanded states, scroll position, or zoom level. This key is used to save and load the view data from the view data store. If you don’t set this key, the persistence is disabled for the associated VisualElement . For more information, refer to View data persistence. |
The following table lists all the C# public property names and their related USS selector.
C# property | USS selector | Description |
---|---|---|
ussClassName |
.unity-tree-view |
The USS class name for TreeView elements. Unity adds this USS class to every instance of the TreeView element. Any styling applied to this class affects every TreeView located beside, or below the stylesheet in the visual tree. |
itemUssClassName |
.unity-tree-view__item |
The USS class name for TreeView item elements. Unity adds this USS class to every item element of the TreeView. Any styling applied to this class affects every item located beside, or below the stylesheet in the visual tree. |
itemToggleUssClassName |
.unity-tree-view__item-toggle |
The USS class name for TreeView item toggle elements. Unity adds this USS class to every item toggle element of the TreeView. Any styling applied to this class affects every item located beside, or below the stylesheet in the visual tree. |
itemIndentsContainerUssClassName |
.unity-tree-view__item-indents |
The USS class name for TreeView indent container elements. Unity adds this USS class to every indent container element of the TreeView. Any styling applied to this class affects every item located beside, or below the stylesheet in the visual tree. |
itemIndentUssClassName |
.unity-tree-view__item-indent |
The USS class name for TreeView indent element. Unity adds this USS class to every indent element of the TreeView. Any styling applied to this class affects every item located beside, or below the stylesheet in the visual tree. |
itemContentContainerUssClassName |
.unity-tree-view__item-content |
The USS class name for TreeView item container elements. Unity adds this USS class to every item container element of the TreeView. Any styling applied to this class affects every item located beside, or below the stylesheet in the visual tree. |
ussClassName |
.unity-collection-view |
The USS class name for BaseVerticalCollectionView elements. Unity adds this USS class to every instance of the BaseVerticalCollectionView element. Any styling applied to this class affects every BaseVerticalCollectionView located beside, or below the stylesheet in the visual tree. |
borderUssClassName |
.unity-collection-view--with-border |
The USS class name for BaseVerticalCollectionView elements with a border. Unity adds this USS class to an instance of the BaseVerticalCollectionView element if the instance’s BaseVerticalCollectionView.showBorder property is set to true. Any styling applied to this class affects every such BaseVerticalCollectionView located beside, or below the stylesheet in the visual tree. |
itemUssClassName |
.unity-collection-view__item |
The USS class name of item elements in BaseVerticalCollectionView elements. Unity adds this USS class to every item element the BaseVerticalCollectionView contains. Any styling applied to this class affects every item element located beside, or below the stylesheet in the visual tree. |
dragHoverBarUssClassName |
.unity-collection-view__drag-hover-bar |
The USS class name of the drag hover bar. Unity adds this USS class to the bar that appears when the user drags an item in the list. Any styling applied to this class affects every BaseVerticalCollectionView located beside, or below the stylesheet in the visual tree. |
dragHoverMarkerUssClassName |
.unity-collection-view__drag-hover-marker |
The USS class name of the drag hover circular marker used to indicate depth. Unity adds this USS class to the bar that appears when the user drags an item in the list. Any styling applied to this class affects every BaseVerticalCollectionView located beside, or below the stylesheet in the visual tree. |
itemDragHoverUssClassName |
.unity-collection-view__item--drag-hover |
The USS class name applied to an item element on drag hover. Unity adds this USS class to the item element when it’s being dragged. Any styling applied to this class affects every BaseVerticalCollectionView item located beside, or below the stylesheet in the visual tree. |
itemSelectedVariantUssClassName |
.unity-collection-view__item--selected |
The USS class name of selected item elements in the BaseVerticalCollectionView. Unity adds this USS class to every selected element in the BaseVerticalCollectionView. The BaseVerticalCollectionView.selectionType property decides if zero, one, or more elements can be selected. Any styling applied to this class affects every BaseVerticalCollectionView item located beside, or below the stylesheet in the visual tree. |
itemAlternativeBackgroundUssClassName |
.unity-collection-view__item--alternative-background |
The USS class name for odd rows in the BaseVerticalCollectionView. Unity adds this USS class to every odd-numbered item in the BaseVerticalCollectionView when the BaseVerticalCollectionView.showAlternatingRowBackgrounds property is set to ContentOnly or All . When the showAlternatingRowBackgrounds property is set to either of those values, odd-numbered items are displayed with a different background color than even-numbered items. This USS class is used to differentiate odd-numbered items from even-numbered items. When the showAlternatingRowBackgrounds property is set to None , the USS class is not added, and any styling or behavior that relies on it’s invalidated. |
listScrollViewUssClassName |
.unity-collection-view__scroll-view |
The USS class name of the scroll view in the BaseVerticalCollectionView. Unity adds this USS class to the BaseVerticalCollectionView’s scroll view. Any styling applied to this class affects every BaseVerticalCollectionView scroll view located beside, or below the stylesheet in the visual tree. |
disabledUssClassName |
.unity-disabled |
USS class name of local disabled elements. |
You can also use the Matching Selectors section in the Inspector or the UI Toolkit Debugger to see which USS selectors affect the components of the VisualElement
at every level of its hierarchy.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
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.