Version: 2022.3
言語: 日本語
プロファイラーモジュールエディター
低レベルのネイティブプラグイン Profiler API

カスタムモジュール詳細パネルの作成

モジュールを選択すると、Profiler ウィンドウの下部にモジュール詳細パネルが表示されます。このセクションをカスタマイズして、モジュールに関連する追加の詳細を表示したり、パフォーマンスデータをカスタム製の可視化で表示したりすることができます。

Profiler モジュールのカスタムのモジュール詳細パネルを作成するには、以下を行います。

モジュール詳細パネルを制御するスクリプトの作成

ProfilerModuleViewController 基本クラスを使用して、Profiler ウィンドウのモジュール詳細パネルをカスタマイズすることができます。これを行うには、特定のモジュールを選択するときに、モジュール詳細パネルに表示されるものを制御するスクリプトを作成します。

モジュール詳細パネルをカスタマイズするために作成するスクリプトは、以下を行う必要があります。

  • 基本コンストラクター base(profilerWindow) を呼び出すビューコントローラーのパブリックコンストラクターを定義します。
  • CreateView をオーバーライドして、カスタムモジュール詳細パネルを構築します。

例:


 public class CustomDetailsViewController : ProfilerModuleViewController
  {   
     public CustomDetailsViewController(ProfilerWindow profilerWindow) : base(profilerWindow) { }

     protected override VisualElement CreateView()
     {
         // Create your UI.
     }
 }

完全なモジュール詳細パネルコントローラースクリプトの例については、モジュール詳細パネルコントローラスクリプト例 を参照してください。

モジュール詳細パネルコントローラースクリプト例

以下のサンプルスクリプトは、モジュール詳細パネルにテキストを表示するラベルを 1 つ描画するモジュール詳細パネルコントローラーを作成します。

このモジュールの詳細パネルコントローラーのスクリプト例では、以下を行います。

  • 取り込みたい値を表示するラベルを定義作成し、このラベルをモジュールの詳細パネルに加えます。
  • モジュール詳細パネルを制御するためのコンストラクターを定義し、CreateView を使用してカスタムモジュール詳細パネルを構築します。
  • ラベルに現在のフレームからのデータを入力し、各フレームの後にラベルを更新します。
  • カウンターの値を文字列として取得し、モジュール詳細パネルに表示することができます。
  • モジュールの詳細パネルに表示するテキストを指定し、Profiler がフレームごとに自動的に更新するように指示します。

 using UnityEditor;
  using UnityEditorInternal;
  using Unity.Profiling.Editor;
  using UnityEngine.UIElements;
 
  public class TankEffectsDetailsViewController : ProfilerModuleViewController
  {
     // Define a label, which will display the total particle count for tank trails in the selected frame.
     Label m_TankTrailParticleCountLabel;


     // Define a constructor for the view controller, which calls the base constructor with the Profiler Window passed from the module.
     public TankEffectsDetailsViewController(ProfilerWindow profilerWindow) : base(profilerWindow) { }
 
     // Override CreateView to build the custom module details panel.6666666667reateView()
     {
         var view = new VisualElement();

         // Create the label and add it to the view.
         m_TankTrailParticleCountLabel = new Label() { style = { paddingTop = 8, paddingLeft = 8 } };
         view.Add(m_TankTrailParticleCountLabel);

         // Populate the label with the current data for the selected frame. 
         ReloadData();

         // Be notified when the selected frame index in the Profiler Window changes, so we can update the label.
         ProfilerWindow.SelectedFrameIndexChanged += OnSelectedFrameIndexChanged;

         return view;
     }

     // Override Dispose to do any cleanup of the view when it is destroyed. This is a standard C# Dispose pattern.
     protected override void Dispose(bool disposing)
     {
         if (!disposing)
             return;

         // Unsubscribe from the Profiler window event that we previously subscribed to.
         ProfilerWindow.SelectedFrameIndexChanged -= OnSelectedFrameIndexChanged;

         base.Dispose(disposing);
     }

     void ReloadData()
     {
         // Retrieve the TankTrailParticleCount counter value from the Profiler as a formatted string.
         var selectedFrameIndexInt32 = System.Convert.ToInt32(ProfilerWindow.selectedFrameIndex);
         var value = ProfilerDriver.GetFormattedCounterValue(selectedFrameIndexInt32, GameStatistics.TanksCategory.Name, GameStatistics.TankTrailParticleCountName);

         // Update the label's text with the value.
         m_TankTrailParticleCountLabel.text = $"The value of '{GameStatistics.TankTrailParticleCountName}' in the selected frame is {value}.";
     }

     void OnSelectedFrameIndexChanged(long selectedFrameIndex)
     {
         // Update the label with the current data for the newly selected frame.
         ReloadData();
     }
 }

モジュール詳細パネルにカスタム UI 要素を作成

Unity の UIToolkit を使用して、モジュ照してください。ール詳細パネル用のカスタム UI を構築することができます。詳細については、UI Toolkit を参照してください。

次のサンプル画像は、カスタム Adaptive Performance モジュールに属するカスタムモジュールの詳細パネルです。

Profiler モジュールへカスタムモジュール詳細パネルを接続

カスタムモジュール詳細パネルを表示するには、Profiler モジュールを選択するときに、モジュール詳細パネルコントロー ラーをインスタンス化する必要があります。これを行うには、 CreateDetailsViewController をオーバーライドして、新しいモジュール詳細パネルコントローラーを作成し、描画します。その後、Unity は、モジュールの詳細パネルを表示するときにこのメソッドを呼び出します。

以下のコード例では、TankEffectsProfilerModule というモジュールのカスタムモジュール詳細パネルをインスタンス化します。


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

    public TankEffectsProfilerModule() : base(k_Counters) { }

    public override ProfilerModuleViewController CreateDetailsViewController()
    {
        return new TankEffectsDetailsViewController(ProfilerWindow);
    }
 }

モジュール詳細パネルで追加カウンターを可視化

モジュールのチャートビューに含まれないカウンターを、モジュール詳細パネルに表示することができます。これは、選択したフレームの追加データを表示したい場合に便利です。

Profiler は、モジュールがアクティブなときに、モジュールのチャートビューに属するすべてのカウンターのカテゴリを自動的に取得します。追加のカウンターを取得するには、モジュールがアクティブなときに特定のカテゴリを取得するように Profiler に指示するスクリプトを記述します。

たとえば、以下のスクリプトでは、autoEnabledCategoryNames コンストラクター引数を使用して、Scripts と Memory カテゴリを指定しています。これにより、モジュールがアクティブなときに、これらのカテゴリが有効になります。

 
 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, ProfilerCategory.Scripts),
        new ProfilerCounterDescriptor(GameStatistics.ShellExplosionParticleCountName, ProfilerCategory.Scripts),
        new ProfilerCounterDescriptor(GameStatistics.TankExplosionParticleCountName, ProfilerCategory.Scripts),
    };

    // Enable the ProfilerCategory.Scripts and ProfilerCategory.Memory categories when the 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) { }
プロファイラーモジュールエディター
低レベルのネイティブプラグイン Profiler API