Skip to main content

ProfilerMarkers Sample

A flock of cubes whose every frame phase shows up in the Profiler under its own name.

The flock simulation whose phases the markers measure.The flock simulation whose phases the markers measure.

The flock simulation whose phases the markers measure.

Open it​

  1. Import the sample: Tools → Aspid 🐍 → FastTools → Welcome → Samples → Import on ProfilerMarkers.
  2. Open Scenes/ProfilerMarkers.unity and Window → Analysis → Profiler, enter Play Mode and select a frame in the CPU module.
  3. In the Hierarchy view, expand PlayerLoop down to Flock.Update (74), under Unity's own Flock.Update() [Invoke] row.

Needs Unity's built-in Physics module; without it the sample scripts do not compile.

Try​

The markers under Flock.Update nest like the code; FlockSimulation.Steering.Agent has 120 calls for 120 agents.The markers under Flock.Update nest like the code; FlockSimulation.Steering.Agent has 120 calls for 120 agents.

The markers under Flock.Update nest like the code; FlockSimulation.Steering.Agent has 120 calls for 120 agents.

  1. The tree follows the using scopes. Under Flock.Update sit FlockSimulation.Step and, next to it, Flock.ApplyTransforms; under Step sit FlockSimulation.Steering and FlockSimulation.Integrate. Nothing wires the nesting, the scopes in the code do:

    public void Step(float deltaTime, float neighborRadius, float maxSpeed)
    {
    using var _ = this.Marker();

    using (this.Marker().WithName("Steering"))
    ComputeSteering(neighborRadius);

    using (this.Marker().WithName("Integrate"))
    Integrate(deltaTime, maxSpeed);
    }
  2. One marker per loop. FlockSimulation.Steering.Agent sits inside the loop over the agents: the Profiler shows one row with Calls equal to the agent count, not a row per agent.

  3. More agents. In Play Mode, raise Count on Flock to 400: the next frame recreates the flock, Calls of FlockSimulation.Steering.Agent becomes 400, and FlockSimulation.Steering takes longer.

  4. Not only MonoBehaviour. FlockSimulation is a plain C# class, and its markers work the same way. The marker in the local function CreateAgent is named after the enclosing method, Flock.InitializeAgents (53): find it in the first frame or in the frame where Count changes, with Calls equal to the number of agents created.

  5. The line number in the name. Every name ends with the line of its call, FlockSimulation.Steering (62), so markers on different lines of one method never share a name, and a moved call changes its number. Search the Profiler for Flock to list every marker of the sample.

  6. Release builds. The generated marker code is wrapped in #if ENABLE_PROFILER: in a player build without Development Build the calls measure nothing and return default.

Where to look​

FileShows
Scripts/FlockSimulation.csMethod-wide, block and per-iteration markers in a plain class
Scripts/Flock.csThe frame entry point, a marker inside a local function

Reference: ProfilerMarkers.