---
title: コードへのプロファイラーカウンターの追加
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/ja-jp/engine/6000.0/manual/analysis/profiler/customizing/adding-information-code/add-counters-code
latest_url: https://docs.unity.com/ja-jp/engine/7000.0/manual/analysis/profiler/customizing/adding-information-code/add-counters-code.md
last_updated: 2026-10-30
generated_at: 2026-11-16T07:31:16.167Z
locale: ja-jp
---

# コードへのプロファイラーカウンターの追加

> プロファイラーカウンターで特定の統計を収集します。

プロファイラーカウンターを追加するには、以下の実行するスクリプトを作成します。

* [新しいカウンタを作成](#creating-and-defining-a-counter)して定義します。
* カウンタの[値の更新](#updating-a-counters-value)。

これらのセクションのコード例では、プロファイラーカウンターを追加して、ゲームオブジェクトのトレイルエフェクトのすべてのインスタンスに対して Unity が作成したパーティクルの総数を追跡します。これらの例では、ゲームオブジェクトの名前は "Tank" です。

## 前提条件##prerequisites

一部の例では、開始前にインストールする必要[があるプロファイリングコアパッケージ](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest)を使用します。Unity プロファイリングコアパッケージはコアパッケージであるため、Package Manager UI では検出できません。パッケージをインストールするには、`com.unity.profiling.core` という[名前で追加](/engine/6000.0/manual/packages-list/managing-packages-window/upm-ui-actions/upm-ui-quick.md)します。

## カウンターの作成と定義##creating-and-defining-a-counter

新しいカウンターを作成するには、以下を行います。

1. 新しいカウンタの値の型を定義するスクリプトを作成します。
2. このタイプに名前と単位を割り当てます。

> **Important:**
>
> カウンターを作成するときは、新しいカウンターが属するプロファイラーカテゴリを指定する必要があります。これを行うには、[`ProfilerCategory`](/engine/6000.0/script-reference/unity/profiling/profilercategory.md) クラスの既存の Unity カテゴリを使用します。以下の例のスクリプトは、既存の`ProfilerCategory.Scripts`カテゴリを使用します。

プロファイラーカウンター API は、プッシュ操作とプル操作をサポートします。プロファイラーにカウンターの値をプッシュするか、プロファイラーはフレームの終了時に値をプルできます。

データの変更頻度が低い場合、例えばフレームごとに 1 回などの場合は、[`ProfilerCounter`](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest?subfolder=/api/Unity.Profiling.ProfilerCounter-1.html) API を使用してカウンターデータをプロファイラーにプッシュします。データがフレームごとに複数回変更される場合は、`ProfilerCounterValue` API を使用します。これにより、プロファイラーは自動的にフレームの終了時に最後の値を取得します。

以下のスクリプト例では、 name `Tank Trail Particles` と[`ProfilerMarkerDataUnit.Count`](/engine/6000.0/script-reference/unity/profiling/profilermarkerdataunit/count.md)単位を指定した [`ProfilerCounterValue`](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest?subfolder=/api/Unity.Profiling.ProfilerCounter-1.html) `TankTrailParticleCount`を定義します。

```lang-cs
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);
}
```

Options `FlushOnEndOfFrame` と `ResetToZeroOnFlush` は、自動的にプロファイラーデータストリームにカウンタを送信し、フレームの終了時に Count 値を 0 にリセットします。

## カウンターの値を更新する##updating-a-counter's-value

カウンタの値を更新するには、定義したカウンタの値を設定する MonoBehaviour スクリプトを作成します。

以下の MonoBehaviour スクリプトは、更新機能で割り当てられたゲームオブジェクトに属するトレイル パーティクルの数をフレームごとにカウントします。これを行うには、`TankTrailParticleCount` というカウンタを使用します。

以下のスクリプト例では、Inspector にトレイルパーティクルシステム (`m_TrailParticleSystem`) というパブリックプロパティも作成します。

```lang-cs
 using UnityEngine;

 クラス TankMovement ：MonoBehaviour
 {
    public ParticleSystem m_TrailParticleSystem;

    void Update()
    {
        GameStats.TankTrailParticleCount.Value += m_TrailParticleSystem.particleCount;
    }
 }
```

### カスタムプロファイラーモジュールへのプロファイラーカウンターの追加##adding-profiler-counters-to-a-custom-profiler-module

コードでカスタムプロファイラーモジュールにプロファイラーカウンターを表示するには、新しい`ProfilerModule`スクリプトを作成し、表示するカウンター、名前、アイコンなどのモジュールのプロパティーを定義する必要があります。この方法については、[コードでプロファイラーモジュール](/engine/6000.0/manual/analysis/profiler/customizing/creating-custom-modules/create-modules.md)を作成する を参照してください。

## 追加リソース##additional-resources

* [コード概要へのプロファイリング情報の追加](/engine/6000.0/manual/analysis/profiler/customizing/adding-information-code/intro.md)
* [コードへのプロファイラーマーカーの追加](/engine/6000.0/manual/analysis/profiler/customizing/adding-information-code/add-markers-code.md)
* [プロファイラーモジュールのカスタマイズ](/engine/6000.0/manual/analysis/profiler/customizing.md)
* [プロファイリングコアパッケージ](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest)
