0
0
Unityframework~8 mins

Sorting layers and order in Unity - Performance & Optimization

Choose your learning style9 modes available
Performance: Sorting layers and order
MEDIUM IMPACT
This affects how quickly and smoothly the game renders objects in the correct visual order, impacting frame rate and visual stability.
Rendering 2D sprites in correct visual order
Unity
Assign sorting layers and orders once during initialization or only when necessary, e.g., on state change.
Minimizes sorting recalculations and reduces redundant rendering work.
📈 Performance Gainavoids repeated re-sorts, improving frame rate and rendering smoothness
Rendering 2D sprites in correct visual order
Unity
foreach (var sprite in sprites) {
    sprite.sortingOrder = CalculateOrder(sprite);
    sprite.sortingLayerName = "DynamicLayer";
}
Changing sorting layers and orders every frame causes the renderer to re-sort and re-draw many objects repeatedly.
📉 Performance Costtriggers multiple re-sorts and repaints per frame, reducing frame rate
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Frequent sorting layer/order changes per frameN/AMultiple re-sorts per frameHigh GPU redraw cost[X] Bad
Sorting layers and orders set once or on changeN/ASingle sort on updateLow GPU redraw cost[OK] Good
Rendering Pipeline
Sorting layers and order determine the sequence in which sprites are sent to the GPU for rendering. Changing these values triggers the engine to re-sort draw calls and update the render queue.
Draw Call Sorting
Render Queue Update
GPU Draw Calls
⚠️ BottleneckDraw Call Sorting stage is most expensive when sorting layers or orders change frequently.
Optimization Tips
1Avoid changing sorting layers and order every frame.
2Set sorting layers and order once during initialization or on state changes.
3Use as few sorting layers as possible to reduce sorting complexity.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance cost of changing sorting layers and order every frame in Unity?
AIt causes the renderer to re-sort and re-draw objects frequently.
BIt increases CPU usage for physics calculations.
CIt reduces memory usage by freeing unused layers.
DIt improves GPU parallelism by batching draw calls.
DevTools: Unity Profiler
How to check: Open Unity Profiler, record during gameplay, and check the Rendering section for draw call counts and sorting overhead.
What to look for: High draw call counts and frequent sorting operations indicate performance issues with sorting layers and order.