Performance: Performance profiling
Performance profiling helps identify which parts of your Unity game slow down frame rendering and cause lag.
Use Unity Profiler to record frame data; analyze CPU, GPU, rendering, and memory usage; identify exact slow functions or assets.
No profiling used; guessing which scripts or assets cause lag; making random changes.| Pattern | CPU Usage | GPU Usage | Frame Drops | Verdict |
|---|---|---|---|---|
| No profiling, guesswork | High and unpredictable | High spikes | Frequent | [X] Bad |
| Profiling with Unity Profiler | Targeted and optimized | Balanced and reduced | Rare or none | [OK] Good |
Profiler.BeginSample("HeavyTask");
for (int i = 0; i < 1000000; i++) { var x = Mathf.Sqrt(i); }
Profiler.EndSample();Profiler.BeginSample("UpdateLoop");
UpdateGameObjects();
// Missing Profiler.EndSample()Pass the test and this pattern moves unknown