Version: 2022.3
Language : English
Customizing the Profiler
Creating Custom Profiler modules

Creating custom Profiler counters

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”.

Defining a counter

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.

Using Profiler categories

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.

Assigning a Profiler counter to a Profiler category

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) { }
}

Updating a counter’s value

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.

Updating the counter’s value script example

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;
    }
 }

Using Profiler counters to profile a release build

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:

Customizing the Profiler
Creating Custom Profiler modules
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961