To display custom metrics in the Unity 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, you must use the ProfilerCounter API in the Unity Profiling Core package.
You can use the Profiling Core API to track a metric in your application. You can display the information that a counter tracks in the Unity Profiler. Use custom Profiler countersPlaced in code with the ProfilerCounter API to track metrics, such as the number of enemies spawned in your game. More info
See in Glossary to compare system metrics and identify performance issues in the Profiler window.
A custom Profiler counter can display data from ProfilerCounter
or ProfilerCounterValue
.
For a complete guide to using the Unity Profiling Core API to create Profiler counters, see the Profiler counters API guide.
To add a profiler counter, create scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary to do the following:
The code examples in these sections add a Profiler counter to track the total number of particles that Unity created for every instance of a GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary’s trail effects. In these examples, the GameObject’s name is “Tank”.
To create a new counter, write a script to define the value type of the new counter, and assign a name and a unit to this type.
When you create a counter you must specify which Profiler category your new counter belongs to. To do this use an existing Unity category. For example, the script example below uses the existing ProfilerCategory.Scripts
category.For more information, see Using Profiler categories
The following example script defines the ProfilerCounterValue TankTrailParticleCount
, with the name “Tank Trail Particles”. This counter has a unit of “Count”:
public static class GameStats
{
public static readonly ProfilerCategory TanksCategory = ProfilerCategory.Scripts;
public const string TankTrailParticleCountName = "Tank Trail Particles";
public static readonly ProfilerCounterValue<int> TankTrailParticleCount =
new ProfilerCounterValue<int>(TanksCategory, TankTrailParticleCountName, ProfilerMarkerDataUnit.Count,
ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
}
The options FlushOnEndOfFrame
and ResetToZeroOnFlush
automatically send the counter to the Profiler data stream and reset the Count value to zero at the end of the frame.
Unity automatically groups Profiler counters into categories based on the type of work the counters profile, for example, Rendering, Scripting, or Animation. You can assign a custom Profiler counter to any of Unity’s profiling categories. For a full list of available Profiler categories, see ProfilerCategory.
A Profiler counter must belong to a Profiler category. You should assign a category to a Profiler counter when you define the counter. To do this, use the ProfilerModule’s optional autoEnabledCategoryNames
constructor argument to assign one or more categories to a Profiler counter. There is an example of this method in the following example code:
using Unity.Profiling;
using Unity.Profiling.Editor;
[System.Serializable]
[ProfilerModuleMetadata("Tank Effects")]
public class TankEffectsProfilerModule : ProfilerModule
{
static readonly ProfilerCounterDescriptor[] k_Counters = new ProfilerCounterDescriptor[]
{
new ProfilerCounterDescriptor(GameStatistics.TankTrailParticleCountName, GameStatistics.TanksCategory),
new ProfilerCounterDescriptor(GameStatistics.ShellExplosionParticleCountName, GameStatistics.TanksCategory),
new ProfilerCounterDescriptor(GameStatistics.TankExplosionParticleCountName, GameStatistics.TanksCategory),
};
// Ensure that both ProfilerCategory.Scripts and ProfilerCategory.Memory categories are enabled when our module is active.
static readonly string[] k_AutoEnabledCategoryNames = new string[]
{
ProfilerCategory.Scripts.Name,
ProfilerCategory.Memory.Name
};
// Pass the auto-enabled category names to the base constructor.
public TankEffectsProfilerModule() : base(k_Counters, autoEnabledCategoryNames: k_AutoEnabledCategoryNames) { }
}
To update the value of a counter, create a MonoBehaviour script that sets the value of a counter you have defined. For more information, see How to pass counter values to the Profiler.
This example MonoBehaviour script counts the number of trail particles that belong to an assigned GameObject every frame in the Update function. To do this, it uses the counter called TankTrailParticleCount
.
The following example script also creates a public property called Trail Particle SystemA component that simulates fluid entities such as liquids, clouds and flames by generating and animating large numbers of small 2D images in the scene. More info
See in Glossary (m_TrailParticleSystem
) in the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary:
using UnityEngine;
class TankMovement : MonoBehaviour
{
public ParticleSystem m_TrailParticleSystem;
void Update()
{
GameStats.TankTrailParticleCount.Value += m_TrailParticleSystem.particleCount;
}
}
When you run your project in a release player, you don’t have access to the Profiler window. However, you can display counters as UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary elements in a release player. This means you can include profiling tools in a released application. To do this, see Getting counter values in players in the Profiler counters API guide.
The following image displays counters in the top left of the sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary using custom UI in a release player:
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.