I created a very simple window with 10K labels. When the mouse enters or exits the window, the performance will be very poor. I see that there are more elements in the UI toolkit debugger, but the performance is not poor. What did I do wrong?
using UnityEditor;
using UnityEngine.UIElements;
public class TestWindow : EditorWindow
{
[MenuItem("Test/Open")]
private static void Open()
{
var window = GetWindow<TestWindow>();
window.Show();
window.Init();
}
private void Init()
{
for (var index = 0; index < 10000; index++)
{
var label = new Label(index.ToString());
rootVisualElement.Add(label);
}
}
}
The ListView is indeed the way to go, it will recycle the elements so that there isn’t a huge amount of elements to go through for events, layout, rendering, etc.
You’re right. Using ListView can solve the problem if the elements are similar. In fact, I need to use more complex and flexible elements, so ListView is not suitable.
This is difficult to answer without more information about what you are trying to do.
The layout and styling phases will be expensive for that many elements. Setting the position to absolute will simplify the layout process for these elements.
If the elements are not all visible on screen at the same time, consider implementing a virtualization system similar to what the ListView does. You would only create as many elements as there are visible on screen, when they go offscreen, they are added to a “waiting list” so that they can be reused when another element goes on screen.
Are the elements moving? If so, set the ve.usageHint = UsageHint.DynamicTransform; hint. This will allow the transformation to occur on the GPU.
Another option would be to have a single VisualElement that draws its own geometry using ve.generateVisualContent.
More details about your particular use-case would help.
I think with so many elements, there will always be performance issues. But the preformance issues of uitoolit start alreay with few elements, when you animate them (UsageHint.DynamicTransform I am afraid does not help much).
So far so, that we decided to use the old UI system for a new WebGL project
UI Toolkit needs to improve its permormance drastically.