Add profiler markersPlaced in code to describe a CPU or GPU event that is then displayed in the Unity Profiler window. Added to Unity code by default, or you can use ProfilerMarker API to add your own custom markers. More info
See in Glossary to your code to view the samples that ProfilerMarker.Begin
, ProfilerMarker.End
, or ProfilerMarker.Auto
generates in the Timeline View and Hierarchy View of the CPU Usage module in the Profiler window:
Some examples use the Profiling Core package, which you must install before you start. The Unity Profiling Core package isn’t discoverable in the Package Manager UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary because it’s a core package. To install the package, add it by its name, which is com.unity.profiling.core
.
To use the ProfilerMarker
API, place the code you want to profile between calls to ProfilerMarker.Begin
and ProfilerMarker.End
. For example:
using UnityEngine;
using Unity.Profiling;
public class ProfilerMarkerExample
{
static readonly ProfilerMarker k_MyCodeMarker = new ProfilerMarker("My Code");
void Update() {
k_MyCodeMarker.Begin();
Debug.Log("This code is being profiled");
k_MyCodeMarker.End();
}
}
Note: Avoid using the /
character in a profilerA window that helps you to optimize your game. It shows how much time is spent in the various areas of your game. For example, it can report the percentage of time spent rendering, animating, or in your game logic. More info
See in Glossary marker’s name as this makes the Profiler window unable to highlight the marker in the CPU Profiler module’s charts view.
Make sure that the code in between the Begin
and End
calls doesn’t exit the scope before End
is called. If the code exits the scope before End
is called, an error message is logged to the Console. To avoid having to call End
before every return, use Auto
so that the sample is ended automatically on leaving the scope. For more information, refer to the section in this documentation on Automatically close profiler marker code blocks.
Unity records and reports the profiled code block’s execution time to the Profiler, and displays it in the CPU Profiler module without the need to use Deep Profiling. It displays it as a new entry in the Hierarchy View of the CPU Profiler module, as follows:
Use ProfilerMarker.Auto
to ensure that ProfilerMarker.End
is automatically called at the end of the code block. The following calls are equivalent:
using Unity.Profiling;
public class MySystemClass
{
static readonly ProfilerMarker k_UpdatePerfMarker = new ProfilerMarker("MySystem.Update");
public void Update()
{
k_UpdatePerfMarker.Begin();
// ...
k_UpdatePerfMarker.End();
using (k_UpdatePerfMarker.Auto())
{
// ...
}
}
}
Unlike the Begin()
and End()
calls, Unity can’t compile out the ProfilerMarker.Auto
call in non-development (Release) builds. It will however instead return null, which only adds minimal overhead.
You can also use ProfilerMarker.Auto
with a using var
and the End
call happens automatically once the current scope ends. This approach minimizes the amount of code you need to change when you add a ProfilerMarker
instance to your code:
using Unity.Profiling;
public class MySystemClass
{
static readonly ProfilerMarker k_UpdatePerfMarker = new ProfilerMarker("MySystem.Update");
public void Update()
{
using var _ = k_UpdatePerfMarker.Auto();
// ...
}
}
Note: Any async await
and any yield
calls within an area marked up with ProfilerMarker
’s aren’t supported and log an error message in the Console, even if you use Auto
.
Sometimes you might want to add context to your code samples to identify specific conditions in which the code runs for a long time.
For example, if your system carries out simulations of objects, you can pass the number of objects with a Profiler sampleA set of data associated with a Profiler marker, that the Profiler has recorded and collected.
See in Glossary. If the Profiler returns an abnormal number along with a long sample duration, that might mean you have to use another thread for simulation, split the CPU work across multiple frames (timeslicing), or adjust your application’s design to prevent frame drops.
ProfilerMarker
supports up to three numeric parameters: ProfilerMarker<TP1>
, ProfilerMarker<TP1, TP2>
and ProfilerMarker<TP1, TP2, TP3>
:
using Unity.Profiling;
public class MySystemClass
{
static readonly ProfilerMarker<int> k_PreparePerfMarker = new ProfilerMarker<int>("MySystem.Prepare", "Objects Count");
static readonly ProfilerMarker<float> k_SimulatePerfMarker = new ProfilerMarker<float>(ProfilerCategory.Scripts, "MySystem.Simulate", "Objects Density");
public void Update(int objectsCount)
{
k_PreparePerfMarker.Begin(objectsCount);
// ...
k_PreparePerfMarker.End();
using (k_SimulatePerfMarker.Auto(objectsCount * 1.0f))
{
// ...
}
}
}
The ProfilerMarker
API supports adding string parameters to your profiler markers. A string parameter can be useful if you want to display the name of the level or file when your application loads level or data files. Use ProfilerMarkerExtension
methods to pass a string parameter along with a Profiler sample:
using Unity.Profiling;
public class MySystemClass
{
static readonly ProfilerMarker k_PreparePerfMarker = new ProfilerMarker("MySystem.Prepare");
public void Prepare(string path)
{
k_PreparePerfMarker.Begin(path);
// ...
k_PreparePerfMarker.End();
}
}
ProfilerMarker
APIDid you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.