# Auto

> Creates a helper struct for the scoped using blocks.

## Definition

* **Type:** Method
* **Namespace:** [Unity.Profiling](/engine/6000.7/script-reference/unity/profiling.md)
* **Assembly:** UnityEngine.CoreModule

## Auto()

Creates a helper struct for the scoped **using** blocks.

```csharp
public ProfilerMarker.AutoScope Auto()
```

### Returns

| Type                                                                                     | Description                                                                                                                                                                                                                                   |
| ---------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [AutoScope](/engine/6000.7/script-reference/unity/profiling/profilermarker/autoscope.md) | IDisposable struct which calls [ProfilerMarker.Begin](/engine/6000.7/script-reference/unity/profiling/profilermarker/begin.md) and [ProfilerMarker.End](/engine/6000.7/script-reference/unity/profiling/profilermarker/end.md) automatically. |

### Remarks

[ProfilerMarker.Begin](/engine/6000.7/script-reference/unity/profiling/profilermarker/begin.md) is called in the constructor and [ProfilerMarker.End](/engine/6000.7/script-reference/unity/profiling/profilermarker/end.md) 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:** [ProfilerMarker.Auto](/engine/6000.7/script-reference/unity/profiling/profilermarker/auto.md) is thread safe and can be used in jobified code.

### Examples

```csharp
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
        }
    }
}
```

## Auto(Object)

Creates a helper struct for the scoped **using** blocks.

```csharp
public ProfilerMarker.AutoScope Auto(Object contextUnityObject)
```

### Parameters

**** (\[Object]\(/engine/6000.7/script-reference/unityengine/object)): Unity object associated with the profiling operation.

### Returns

| Type                                                                                     | Description                                                                                                                                                                                                                                   |
| ---------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [AutoScope](/engine/6000.7/script-reference/unity/profiling/profilermarker/autoscope.md) | IDisposable struct which calls [ProfilerMarker.Begin](/engine/6000.7/script-reference/unity/profiling/profilermarker/begin.md) and [ProfilerMarker.End](/engine/6000.7/script-reference/unity/profiling/profilermarker/end.md) automatically. |

### Remarks

[ProfilerMarker.Begin](/engine/6000.7/script-reference/unity/profiling/profilermarker/begin.md) is called in the constructor and [ProfilerMarker.End](/engine/6000.7/script-reference/unity/profiling/profilermarker/end.md) 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:** [ProfilerMarker.Auto](/engine/6000.7/script-reference/unity/profiling/profilermarker/auto.md) is thread safe and can be used in jobified code.

### Examples

```csharp
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
        }
    }
}
```

## Auto(string)

Creates a helper struct for the scoped **using** blocks.

```csharp
public ProfilerMarker.AutoScope Auto(string metadata)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): Additional contextual metadata string to attach to the profiling sample.

### Returns

| Type                                                                                     | Description                                                                                                                                                                                                                                   |
| ---------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [AutoScope](/engine/6000.7/script-reference/unity/profiling/profilermarker/autoscope.md) | IDisposable struct which calls [ProfilerMarker.Begin](/engine/6000.7/script-reference/unity/profiling/profilermarker/begin.md) and [ProfilerMarker.End](/engine/6000.7/script-reference/unity/profiling/profilermarker/end.md) automatically. |

### Remarks

[ProfilerMarker.Begin](/engine/6000.7/script-reference/unity/profiling/profilermarker/begin.md) is called in the constructor and [ProfilerMarker.End](/engine/6000.7/script-reference/unity/profiling/profilermarker/end.md) 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:** [ProfilerMarker.Auto](/engine/6000.7/script-reference/unity/profiling/profilermarker/auto.md) is thread safe and can be used in jobified code.

### Examples

```csharp
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
        }
    }
}
```
