# CreateMarker

> Constructs a new Profiler marker handle for code instrumentation.

## Definition

* **Type:** Method
* **Namespace:** [Unity.Profiling.LowLevel.Unsafe](/engine/6000.6/script-reference/unity/profiling/lowlevel/unsafe.md)
* **Assembly:** UnityEngine.CoreModule

## CreateMarker(string, ushort, MarkerFlags, int)

Constructs a new Profiler marker handle for code instrumentation.

```csharp
public static nint CreateMarker(string name, ushort categoryId, MarkerFlags flags, int metadataCount)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): A marker name.**** (\[ushort]\(https\://learn.microsoft.com/dotnet/api/system.uint16)): A profiler category identifier.**** (\[MarkerFlags]\(/engine/6000.6/script-reference/unity/profiling/lowlevel/markerflags)): The marker flags.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The metadata parameters count, or 0 if no parameters are expected.

### Returns

| Type                                                           | Description                       |
| -------------------------------------------------------------- | --------------------------------- |
| [IntPtr](https://learn.microsoft.com/dotnet/api/system.intptr) | Returns the marker native handle. |

### Remarks

Use profiler marker handle with [ProfilerUnsafeUtility](/engine/6000.6/script-reference/unity/profiling/lowlevel/unsafe/profilerunsafeutility.md) methods only.

The *categoryId* parameter associates the marker with one of the predefined categories in [ProfilerCategory](/engine/6000.6/script-reference/unity/profiling/profilercategory.md) struct.

**Note:** Markers created with *CreateMarker* automatically get [MarkerFlags.Script](/engine/6000.6/script-reference/unity/profiling/lowlevel/markerflags/script.md) flag.

```csharp
using Unity.Collections.LowLevel.Unsafe;
using Unity.Profiling.LowLevel;
using Unity.Profiling.LowLevel.Unsafe;
using System;

class Example
{
    static readonly IntPtr markerHandle = ProfilerUnsafeUtility.CreateMarker("MyMarker", ProfilerUnsafeUtility.CategoryScripts, MarkerFlags.Default, 0);
    static unsafe void DoWork(int num)
    {
        ProfilerUnsafeUtility.BeginSample(markerHandle);
        //...
        ProfilerUnsafeUtility.EndSample(markerHandle);
    }
}
```

Use *CreateMarker* to set metadata parameter names when passing metadata with :ref::BeginSampleWithMetadata method.

```csharp
using Unity.Collections.LowLevel.Unsafe;
using Unity.Profiling;
using Unity.Profiling.LowLevel;
using Unity.Profiling.LowLevel.Unsafe;
using System;

class Example
{
    static IntPtr MakeMarkerWithIntMetadata(string name, string paramName)
    {
        var handle = ProfilerUnsafeUtility.CreateMarker(name, ProfilerUnsafeUtility.CategoryScripts, MarkerFlags.Default, 1);
        ProfilerUnsafeUtility.SetMarkerMetadata(handle, 0, paramName, (byte)ProfilerMarkerDataType.Int32, (byte)ProfilerMarkerDataUnit.Count);
        return handle;
    }

    static readonly IntPtr markerHandle = MakeMarkerWithIntMetadata("MyMarker", "Work Idx");

    static unsafe void DoWork(int num)
    {
        var metadata = stackalloc ProfilerMarkerData[1];
        metadata[0].Type = (byte)ProfilerMarkerDataType.Int32;
        metadata[0].Size = (uint)UnsafeUtility.SizeOf<int>();
        metadata[0].Ptr = UnsafeUtility.AddressOf(ref num);
        ProfilerUnsafeUtility.BeginSampleWithMetadata(markerHandle, 1, metadata);
        //...
        ProfilerUnsafeUtility.EndSample(markerHandle);
    }
}
```

Additional Resources: [ProfilerCategory](/engine/6000.6/script-reference/unity/profiling/profilercategory.md), [MarkerFlags](/engine/6000.6/script-reference/unity/profiling/lowlevel/markerflags.md).

## CreateMarker(char\*, int, ushort, MarkerFlags, int)

Constructs a new Profiler marker handle for code instrumentation.

```csharp
public static nint CreateMarker(char* name, int nameLen, ushort categoryId, MarkerFlags flags, int metadataCount)
```

### Parameters

**** (\[char\*]\(https\://learn.microsoft.com/dotnet/api/system.char)): A marker name.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Marker name string length.**** (\[ushort]\(https\://learn.microsoft.com/dotnet/api/system.uint16)): A profiler category identifier.**** (\[MarkerFlags]\(/engine/6000.6/script-reference/unity/profiling/lowlevel/markerflags)): The marker flags.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The metadata parameters count, or 0 if no parameters are expected.

### Returns

| Type                                                           | Description                       |
| -------------------------------------------------------------- | --------------------------------- |
| [IntPtr](https://learn.microsoft.com/dotnet/api/system.intptr) | Returns the marker native handle. |

### Remarks

Use profiler marker handle with [ProfilerUnsafeUtility](/engine/6000.6/script-reference/unity/profiling/lowlevel/unsafe/profilerunsafeutility.md) methods only.

The *categoryId* parameter associates the marker with one of the predefined categories in [ProfilerCategory](/engine/6000.6/script-reference/unity/profiling/profilercategory.md) struct.

**Note:** Markers created with *CreateMarker* automatically get [MarkerFlags.Script](/engine/6000.6/script-reference/unity/profiling/lowlevel/markerflags/script.md) flag.

```csharp
using Unity.Collections.LowLevel.Unsafe;
using Unity.Profiling.LowLevel;
using Unity.Profiling.LowLevel.Unsafe;
using System;

class Example
{
    static readonly IntPtr markerHandle = ProfilerUnsafeUtility.CreateMarker("MyMarker", ProfilerUnsafeUtility.CategoryScripts, MarkerFlags.Default, 0);
    static unsafe void DoWork(int num)
    {
        ProfilerUnsafeUtility.BeginSample(markerHandle);
        //...
        ProfilerUnsafeUtility.EndSample(markerHandle);
    }
}
```

Use *CreateMarker* to set metadata parameter names when passing metadata with :ref::BeginSampleWithMetadata method.

```csharp
using Unity.Collections.LowLevel.Unsafe;
using Unity.Profiling;
using Unity.Profiling.LowLevel;
using Unity.Profiling.LowLevel.Unsafe;
using System;

class Example
{
    static IntPtr MakeMarkerWithIntMetadata(string name, string paramName)
    {
        var handle = ProfilerUnsafeUtility.CreateMarker(name, ProfilerUnsafeUtility.CategoryScripts, MarkerFlags.Default, 1);
        ProfilerUnsafeUtility.SetMarkerMetadata(handle, 0, paramName, (byte)ProfilerMarkerDataType.Int32, (byte)ProfilerMarkerDataUnit.Count);
        return handle;
    }

    static readonly IntPtr markerHandle = MakeMarkerWithIntMetadata("MyMarker", "Work Idx");

    static unsafe void DoWork(int num)
    {
        var metadata = stackalloc ProfilerMarkerData[1];
        metadata[0].Type = (byte)ProfilerMarkerDataType.Int32;
        metadata[0].Size = (uint)UnsafeUtility.SizeOf<int>();
        metadata[0].Ptr = UnsafeUtility.AddressOf(ref num);
        ProfilerUnsafeUtility.BeginSampleWithMetadata(markerHandle, 1, metadata);
        //...
        ProfilerUnsafeUtility.EndSample(markerHandle);
    }
}
```

Additional Resources: [ProfilerCategory](/engine/6000.6/script-reference/unity/profiling/profilercategory.md), [MarkerFlags](/engine/6000.6/script-reference/unity/profiling/lowlevel/markerflags.md).
