2023-09-05: How to Profile FPS in Unity

How to profile FPS with Unity

Let’s say my game is running like dog poop and I need to improve the FPS. (20,000 shopping carts moving around aimlessly within a boundary) Alt Text

  1. In Unity: Window -> analysis -> Profiler (or Ctrl+7)
  2. Start the game (make sure the red recording toggle is switched on!)
  3. Clicking on the graph on the Profile Mechanic (E.g. CPU Usage) will open the Timeline:

Alt Text

  1. Identify the biggest bottleneck using the graph section
  2. Identify the culprit script(s) using the timeline section.
  3. Add new tags in the culprit script to profile a snippet of code, then repeat the test.
         Profiler.BeginSample("Move");
         Move();
         Profiler.EndSample();
    
  4. Change the “timeline” to “Hierarchy” view and drill down of the script: Alt Text

Side note: Formula for calculating the “Frame Budget” (also called Time Budget):

  • Targeting 30FPS = (1000 ms / 30 fps) = time budget of 33.33 ms per frame
  • Targeting 60FPS = (1000 ms / 60 fps) = time budget of 16.33 ms per frame