Unity Profiler でカスタム指標を表示するには、Unity Profiling Core パッケージに含まれる ProfilerCounter API を使用する必要があります。
Profiling Core API を使用して、アプリケーションの指標を追跡することができます。カウンターが追跡する情報は、Unity Profiler で表示できます。カスタムプロファイラーカウンターを使用して、Profiler ウィンドウでシステム指標を比較し、パフォーマンスの問題を特定します。
カスタムプロファイラーカウンターは、ProfilerCounter
または ProfilerCounterValue
からデータを表示することができます。
Unity Profiling Core API を使用してプロファイラーカウンターを作成する完全なガイドについては、Profiler counters API guide を参照してください。
プロファイラーカウンターを加えるには、以下を行うスクリプトを作成します。
これらのセクションのコード例では、ゲームオブジェクトのトレイル効果のインスタンスごとに Unity が作成したパーティクルの総数を追跡するプロファイラーカウンターを加えます。これらの例では、ゲームオブジェクトの名前は “Tank ”です。
新しいカウンターを作成するには、新しいカウンターの値の型を定義し、この型に名前と単位を割り当てるスクリプトを記述します。
カウンターを作成するときに、新しいカウンターが属する Profiler カテゴリ を指定する必要があります。これを行うには、既存の Unity カテゴリを使用します。例えば、以下のスクリプト例では、既存の ProfilerCategory.Scripts
カテゴリを使用します。詳細については、Profiler カテゴリの使用 を参照してください。
以下のサンプルスクリプトは、ProfilerCounterValue TankTrailParticleCount
を、Tank Trail Particles という名で定義しています。このカウンターの単位は “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);
}
オプション FlushOnEndOfFrame
と ResetToZeroOnFlush
は、自動的にカウンターを Profiler データストリームに送り、フレームの終了時に Count 値を 0 にリセットします。
Unity は、プロファイラーカウンターを、カウンターがプロファイルする作業の種類、たとえばレンダリング、スクリプト、アニメーションに基づいて自動的にカテゴリにグループ化します。カスタムのプロファイラーカウンターは、Unity のプロファイリングカテゴリのいずれかに割り当てることができます。利用可能なプロファイラーカテゴリの完全なリストについては、ProfilerCategory を参照してください。
プロファイラーカウンターは、プロファイラーカテゴリに属している必要があります。カウンターを定義するときに、プロファイラーカウンターにカテゴリーを割り当てる必要があります。これを行うには、ProfilerModule のオプションの autoEnabledCategoryNames
コンストラクターの引数を使って、1 つ以上のカテゴリをプロファイラーカウンターに割り当てます。以下のサンプルコードに、この方法の例があります。
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) { }
}
カウンターの値を更新するには、定義したカウンターの値を設定する MonoBehaviour スクリプトを作成します。詳しくは、プロファイラーにカウンターの値を渡す方法 を参照してください。
この MonoBehaviour スクリプトの例では、Update 関数において、割り当てられたゲームオブジェクトに属するトレイルパーティクルの数をフレームごとに数えています。そのために TankTrailParticleCount
というカウンターを使用しています。
また、以下のサンプルスクリプトでは、Trail Particle System (m_TrailParticleSystem
) というパブリックプロパティを Inspactor に作成しています。
using UnityEngine;
class TankMovement : MonoBehaviour
{
public ParticleSystem m_TrailParticleSystem;
void Update()
{
GameStats.TankTrailParticleCount.Value += m_TrailParticleSystem.particleCount;
}
}
リリースされたプレイヤーでプロジェクトを実行する場合、Profiler ウィンドウにはアクセスできません。しかし、リリースされたプレイヤーでは、UI 要素としてカウンターを表示できます。つまり、リリースされたアプリケーションにプロファイリングツールを入れることができます。これには、Profiler counters API guide の Getting counter values in players を参照してください。
以下の画像は、リリースプレーヤーでカスタム UI を使用して、シーンの左上にカウンターを表示しています。
Did 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.