0
0
Unityframework~8 mins

Slider and progress bars in Unity - Performance & Optimization

Choose your learning style9 modes available
Performance: Slider and progress bars
MEDIUM IMPACT
This concept affects the rendering performance and responsiveness of UI elements that update frequently, impacting smoothness and input delay.
Updating a progress bar value every frame during a loading operation
Unity
void Update() {
    float newValue = GetLoadingProgress();
    if (Mathf.Abs(progressBar.value - newValue) > 0.01f) {
        progressBar.value = newValue;
    }
}
Only updates the UI when the value changes significantly, reducing redundant redraws and layout recalculations.
📈 Performance Gainreduces reflows and repaints to only necessary updates, improving frame rate and responsiveness
Updating a progress bar value every frame during a loading operation
Unity
void Update() {
    progressBar.value = GetLoadingProgress();
}
Updating the progress bar every frame triggers continuous UI redraws and layout recalculations, causing unnecessary CPU and GPU load.
📉 Performance Costtriggers 60+ reflows and repaints per second, increasing CPU/GPU usage and reducing frame rate
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Update every frame without checkHigh (many UI updates)60+ per secondHigh (continuous redraws)[X] Bad
Update only on significant value changeLow (few UI updates)Few per secondLow (minimal redraws)[OK] Good
Rendering Pipeline
When a slider or progress bar value changes, Unity recalculates the UI layout and redraws the affected elements. Frequent changes cause repeated layout and paint operations.
Layout Calculation
UI Rebuild
Render
⚠️ BottleneckUI Rebuild and Render stages due to frequent value changes causing repeated redraws
Core Web Vital Affected
INP
This concept affects the rendering performance and responsiveness of UI elements that update frequently, impacting smoothness and input delay.
Optimization Tips
1Avoid updating slider or progress bar values every frame without change checks.
2Batch UI updates and only update when values change significantly.
3Use Unity Profiler's UI module to monitor and optimize UI update costs.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance issue when updating a progress bar every frame without checking for value changes?
AIt causes unnecessary UI redraws and layout recalculations
BIt increases memory usage by storing too many values
CIt blocks the main thread from loading assets
DIt causes the slider to lose user input focus
DevTools: Unity Profiler
How to check: Open Unity Profiler, select UI module, run scene with slider/progress bar, observe UI rebuild and rendering times during updates
What to look for: Look for high UI rebuild or rendering times indicating frequent redraws; lower times indicate efficient updates