Performance: Sorting layers and order
This affects how quickly and smoothly the game renders objects in the correct visual order, impacting frame rate and visual stability.
Assign sorting layers and orders once during initialization or only when necessary, e.g., on state change.
foreach (var sprite in sprites) { sprite.sortingOrder = CalculateOrder(sprite); sprite.sortingLayerName = "DynamicLayer"; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Frequent sorting layer/order changes per frame | N/A | Multiple re-sorts per frame | High GPU redraw cost | [X] Bad |
| Sorting layers and orders set once or on change | N/A | Single sort on update | Low GPU redraw cost | [OK] Good |
Sorting Layers in Unity?sortingOrder.spriteRenderer.sortingOrder is a valid property; others are incorrect or do not exist.spriteA.sortingOrder = 3;spriteB.sortingOrder = 7;spriteX on sorting layer "Background" order 0, spriteY on sorting layer "Foreground" order 0, and spriteZ on sorting layer "Background" order 5.Pass the test and this pattern moves unknown