Unity3D: Performance considerations for runtime UI

Recommended: Add NSDT Scene Editor to your 3D toolchain
3D Toolset: NSDT Jianshi Digital Twin

Performance Considerations for Runtime UI

This page describes how to improve the performance of the runtime user interface
.

Optimize data storage

You can use  usageHints to set how elements are used at runtime so that data storage can be optimized accordingly. For example:

visualElement.usageHints = UsageHints.DynamicTransform;

The following table describes with examples which attributes to use for which elements:

Has the following elements use this example
Frequently changes its location or transitions UsageHints.DynamicTransform If you change the , or element, set  UsageHints.DynamicTransform on that element .style.leftstyle.topstyle.position
Many child elements that change position or transition frequentlyDynamicTransform UsageHints.GroupTransform In  ShaderGraph , each node uses, set UsageHints.GroupTransform  on the view container  .DynamicTransform
Animating built-in style colors UsageHints.DynamicColor If you change the element's, or any border color (such as ), set UsageHints.DynamicColor on that element  .style.backgroundColorstyle.unityBackgroundImageTintColorstyle.borderLeftColor

Consider device capabilities

Some Android devices and WebGL
cannot partially patch the index buffer. If your audience uses such devices, or if you target WebGL, UI Kit rendering will still work, but performance may be reduced. To avoid performance degradation, do not animate too many elements at the same time and profile them on the device.

Avoid grid tessellation

It is computationally expensive to build mesh tessellation
visual elements
. Every time an element's size (width/height) changes, its geometry is rebuilt, which can be a problem with animation or frequent resizing.

In general, transforms and textures are not good choices in terms of flexibility and reuse. However, for better performance when animating, do the following:

  • Use transform instead of width or other layout properties
  • Use textures or 2D sprites
    instead of rounded corners and borders

Controlling textures for dynamic atlases

To reduce the number of batches interrupted by texture changes and achieve good performance, use atlases to group together textures that are used simultaneously. You can achieve this using any of the following methods:

  • Use a sprite atlas
    . Using this method, you have more control over your sprites, but you will need to manually create the sprite atlas.
  • Use a motion picture gallery. The UI Toolkit automatically adds textures to or removes textures from the Dynamic Atlas when visual elements reference it.

Verify textures in dynamic atlases

To limit the number of draw calls when combining textures together using a dynamic atlas, make sure the textures go into a dynamic atlas. To verify, use the frame debugger. The frame debugger helps you observe texture changes and infer batch interrupts.

The following example verifies that the dynamic atlas contains all textures in the runtime UI.

The sample dynamic atlas window contains all the textures in the runtime UI.

Apply built-in filters

The dynamic atlas texture starts at the specified minimum size and grows as needed, doubling horizontally or vertically, up to the specified maximum size. You can define the minimum and maximum gallery size in the panel settings resource. You can also use the filters in the dynamic atlas to decide whether to add subtextures to the atlas.

To enable or disable a filter, in the Inspector window of the Panel Settings asset, select an option from the Motion Atlas Settings > Active Filter drop-down list.

Use custom filters

You can PanelSettings.dynamicAtlasSettings.customFilter assign add or relax constraints globally or on a per-texture basis.

The following custom filter example bypasses  the large texture in the Size  filter while keeping  the Size  filter active for other textures:

using UnityEngine;
using UnityEngine.UIElements;

class MyCustomFilter : MonoBehaviour
{
    public PanelSettings panelSettings;
    public Texture2D largeTexture;

    void OnEnable() { panelSettings.dynamicAtlasSettings.customFilter = Filter; }

    void OnDisable() { panelSettings.dynamicAtlasSettings.customFilter = null; }

    bool Filter(Texture2D texture, ref DynamicAtlasFilters filtersToApply)
    {
        if (texture == largeTexture)
        {
            // Disable the Size check for this one.
            filtersToApply &= ~DynamicAtlasFilters.Size;
        }
        return true;
    }
}

The translation is organized and translated by the 3D modeling learning studio , please indicate the source for reprinting!

Guess you like

Origin blog.csdn.net/jianshi2023/article/details/130859842