---
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/creating-custom-counters
latest_url: https://docs.unity.com/ja-jp/engine/7000.0/manual/analysis/profiler/customizing/adding-information-code/creating-custom-counters.md
last_updated: 2026-10-30
generated_at: 2026-11-16T08:11:38.024Z
locale: ja-jp
---

# プロファイラーカウンターの可視化

> カスタムプロファイラーモジュールにプロファイラーカウンターを追加する。

[コードに追加](/engine/6000.0/manual/analysis/profiler/customizing/adding-information-code/add-counters-code.md)したプロファイラーカウンターは、プロファイラーウィンドウまたはアプリケーションのビルドで表示できます。

## プロファイラー ウィンドウでのカスタムカウンターの可視化##visualizing-custom-counters-in-the-profiler-window

プロファイラーウィンドウで`ProfilerCounter`または`ProfilerCounterValue`が生成するデータを表示するには、[カスタムプロファイラーモジュール](/engine/6000.0/manual/analysis/profiler/customizing.md)を使用します。これは、他のシステムメトリクスとの関係を視覚的に認識し、パフォーマンスの問題を迅速に特定するのにヘルプます。

### コードへのメトリクスの追加##adding-metrics-to-your-code

以下の例は、GameManager クラスが高レベルのロジックを処理し、敵について知っていることを前提としています。カウンターの値を更新するには、`Update` メソッドまたは `LateUpdate` メソッドで （ロジックが敵のスポーンまたは破棄とともに実行されるタイミングに応じて）、`Sample` メソッドを使用して敵のカウント値をプロファイラーにプッシュします。

```lang-cs
using UnityEngine;
using Unity.Profiling;

class GameManager :MonoBehaviour
{
    Enemy[] m_Enemies;

    void Update()
    {
        GameStats.EnemyCount.Sample(m_Enemies.Length);
    }
}
```

弾数をプロファイラーに渡すために、この例では弾のライフライフサイクルを管理するスクリプトと仮定しています。その後、ゲーム内の現在の弾の流れに関する正確な情報が得られる`OnDestroy`に、`Awake`で`GameStats.BulletCount`値を増やし、値を減らします。

```lang-cs
using UnityEngine;
using Unity.Profiling;

public class Shell :MonoBehaviour
{
    void Awake()
    {
        GameStats.BulletCount.Value += 1;
    }
    void OnDestroy()
    {
        GameStats.BulletCount.Value -= 1;
    }
}
```

> **Note:**
>
> Unity は、開発ビルド以外のビルドで`ProfilerCounter`と`ProfilerCounterValue`の両方をコンパイルします。

### プロファイラー ウィンドウにカスタムモジュールを追加する##add-a-custom-module-to-the-profiler-window

[プロファイラーモジュールエディタ](/engine/6000.0/manual/analysis/profiler/customizing/creating-custom-modules/module-editor.md)を使用して、ビルトインまたは新しく追加したカウンターを可視化用に選択します。プロファイラーモジュールエディタを開く

1. プロファイラーウィンドウ (**ウィンドウ> 分析> プロファイラー**) を開きます。
2. ウィンドウの左上にあるプロファイラーモジュールのドロップダウンを選択します。歯車のアイコンをクリックすると、プロプロファイラーモジュールエディターが新しいウィンドウで開きます。
3. **Add** を選択し、ウィンドウの右側の**ユーザー** リストから表示するカウンタを選択します。

> **Important:**
>
> ウィンドウドしていない場合、作成したカウンターは、プロファイラーモジュールエディターをロードしたときに使用可能なカウンターペインに表示されません。カスタムカウンターを表示するには、プロファイラーで発行したカウンターを含むデータを [キャプチャまたはロード](/engine/6000.0/manual/profiler-collect-data.md) し、プロファイラーモジュールエディタを再度開きます。

その後、プロファイラー ウィンドウで他のカウンターと一緒にデータを表示できます。詳細については、[カスタムプロファイラーモジュール作成](/engine/6000.0/manual/analysis/profiler/customizing/creating-custom-modules.md) を参照してください。


**Module with custom counters in Profiler Window.:**
![](/api/media?file=/engine/6000.0/media/images/profiler-window-custom-module.png)

> **Note:**
>
> 静的として宣言されたカウンタは、型の初期化時に C# コードで動的に初期化され、実際に初期化して使用するまで使用できない場合があります。これは編集モードと再生モードの両方に適用されます。プロファイラーモジュールエディターにカウンターが表示されない場合は、まず数フレームの間、プロファイラーでデータを記録してください。(/engine/6000.0/manual/analysis/profiler/profiling-applications/profiling-target-device)

## ビルドされたプレイヤーでのプロファイラーカウンターの可視化##visualizing-profiler-counters-in-a-built-player

ビルドされたプレイヤーでプロジェクトを実行すると、プロファイラー ウィンドウにアクセスできません。ただし、作成したプレイヤーでは、UI 要素としてカウンタをディスプレイできます。つまり、アプリケーションのビルドにプロファイリングツールを含めることができます。

以下の画像は、ビルドされたプレイヤーのカスタムUIを使用して、シーンの左上にカウンターを表示しています。


**Tanks game with custom information overlaid:**
![](/api/media?file=/engine/6000.0/media/images/Tanks-Runtime-Stats.png)

> **Note:**
>
> リリースプレイヤー (開発用以外のビルド) では、すべてのプロファイラーカウンターを使用できるわけではありません。[`ProfilerRecorder.Valid`](/engine/6000.0/script-reference/unity/profiling/profilerrecorder/valid.md) を使用して、データが利用可能かどうか、およびプロファイラーがデータを記録できるかどうかを決定します。または、[`ProfilerRecorderHandle.GetAvailable`](/engine/6000.0/script-reference/unity/profiling/lowlevel/unsafe/profilerrecorderhandle/getavailable.md) を使用して、使用可能なすべてのプロファイラー統計を列挙できます。

### プレイヤーでのカウンター値の取得##getting-counter-values-in-players

プロファイラーカウンターは、重要なゲームまたはエンジンシステムメトリクスに関する情報を提供します。継続的なインテグレーション設定がある場合、またはテスト再生中にアプリケーション内のキーパフォーマンスメトリクスを可視化する場合は、[`ProfilerRecorder`](/engine/6000.0/script-reference/unity/profiling/profilerrecorder.md) API を使用してカスタムおよびビルトインプロファイラーのカウンター値を取得できます。

例えば、以下のスクリプトは、フレーム時間、Mono/IL2CPP ヒープサイズ、およびアプリケーションが使用する総メモリー量を表示します。

```lang-cs
using System.Collections.Generic;
using System.Text;
using Unity.Profiling;
using UnityEngine;

public class StatsScript :MonoBehaviour
{
    string statsText;
    ProfilerRecorder systemMemoryRecorder;
    ProfilerRecorder gcMemoryRecorder;
    ProfilerRecorder mainThreadTimeRecorder;

    double GetRecorderFrameAverage(ProfilerRecorder recorder)
    {
        var samplesCount = recorder.Capacity;
        if (samplesCount == 0)
            return 0;

        double r = 0;
        安全ではない
        {
            var samples = stackalloc ProfilerRecorderSample[samplesCount];
            recorder.CopyTo(samples, samplesCount);
            for (var i = 0; i < samplesCount; ++i)
                r += samples[i].Value;
            r /= samplesCount;
        }

        return r;
    }

    void OnEnable()
    {
        systemMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "System Used Memory");
        gcMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Reserved Memory");
        mainThreadTimeRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Internal, "Main Thread", 15);
    }

    void OnDisable()
    {
        systemMemoryRecorder.Dispose();
        gcMemoryRecorder.Dispose();
        mainThreadTimeRecorder.Dispose();
    }

    void Update()
    {
        var sb = new StringBuilder(500);
        sb.AppendLine($"Frame Time: {GetRecorderFrameAverage(mainThreadTimeRecorder) * (1e-6f):F1} ms");
        sb.AppendLine($"GC Memory: {gcMemoryRecorder.LastValue / (1024 * 1024)} MB");
        sb.AppendLine($"System Memory: {systemMemoryRecorder.LastValue / (1024 * 1024)} MB");
        statsText = sb.ToString();
    }

    void OnGUI()
    {
        GUI.TextArea(new Rect(10, 30, 250, 50), statsText);
    }
}
```

> **Important:**
>
> `ProfilerRecorder.Dispose` を使用して、`ProfilerRecorder` に関連付けられたアンマネージリソースを解放します。

## エディタのプロファイラーフレームデータからのカウンター値の取得##getting-counter-values-from-profiler-frame-data-in-the-editor

エディターでプロファイラーフレームデータを処理するときにプロファイラーカウンターの値を取得するには、[`FrameDataView`](/engine/6000.0/script-reference/unityeditor/profiling/framedataview.md) API を使用します。

[`FrameDataView.GetCounterValueAsInt`](/engine/6000.0/script-reference/unityeditor/profiling/framedataview/getcountervalueasint.md)、[`FrameDataView.GetCounterValueAsLong`](/engine/6000.0/script-reference/unityeditor/profiling/framedataview/getcountervalueaslong.md)、[`FrameDataView.GetCounterValueAsFloat`](/engine/6000.0/script-reference/unityeditor/profiling/framedataview/getcountervalueasfloat.md)、および [`FrameDataView.GetCounterValueAsDouble`](/engine/6000.0/script-reference/unityeditor/profiling/framedataview/getcountervalueasdouble.md) を使用して、次のように特定のカウンタのフレーム値を取得できます。

```lang-cs
using UnityEditor.Profiling;

クラス例
{
    static int ExtractMyCounterValue(FrameDataView frameData, string counterName)
    {
        var counterMarkerId = frameData.GetMarkerId(counterName);
        return frameData.GetCounterValueAsInt(counterMarkerId);
    }
}
```

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

* [コードへのプロファイラーカウンターの追加](/engine/6000.0/manual/analysis/profiler/customizing/adding-information-code/add-counters-code.md)
* [プロファイラーモジュールエディタリファレンス](/engine/6000.0/manual/analysis/profiler/customizing/creating-custom-modules/module-editor.md)
