| Parameter | Description |
|---|---|
| contextUnityObject | Unity object associated with the profiling operation. |
| metadata | Additional contextual metadata string to attach to the profiling sample. |
Creates a helper struct for the scoped using blocks.
Begin is called in the constructor and End in the Dispose method.
When a contextUnityObject is provided, the Profiler will associate the profiling sample with the specified Unity object. This allows you to see which specific objects are associated with performance samples in the Profiler window, making it easier to identify performance bottlenecks related to specific GameObjects, Components, or other Unity objects.
When metadata is provided, it attaches additional contextual information to the profiling sample. This string appears in the Profiler window alongside the marker name, allowing you to track different execution paths or parameter values without creating separate markers. This is particularly useful for profiling methods that handle different data or scenarios.
Note: Auto is thread safe and can be used in jobified code.
using Unity.Profiling; using UnityEngine;
public class MySystemClass : MonoBehaviour { static ProfilerMarker s_SimulatePerfMarker = new ProfilerMarker("MySystem.Simulate"); static ProfilerMarker s_ProcessItemPerfMarker = new ProfilerMarker("MySystem.ProcessItem");
public void UpdateLogic() { // Basic usage using (s_SimulatePerfMarker.Auto()) { // ... }
// Associate the profiler sample with a specific object using (s_SimulatePerfMarker.Auto(this)) { // ... } }
public void ProcessItem(string itemType) { // Add metadata to distinguish different code paths using (s_ProcessItemPerfMarker.Auto(itemType)) { // Process based on itemType // The metadata helps identify which type is causing performance issues } } }