コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Unity 2020.2b 新機能 - Profiler の情報をランタイムで取得できる「ProfilerRecorder」

使用例

using System.Text;
using Unity.Profiling;
using UnityEngine;

public class Example : MonoBehaviour
{
    private string           m_text;
    private ProfilerRecorder m_totalUsedMemoryRecorder;
    private ProfilerRecorder m_totalReservedMemoryRecorder;
    private ProfilerRecorder m_gcUsedMemoryRecorder;
    private ProfilerRecorder m_gcReservedMemoryRecorder;
    private ProfilerRecorder m_gfxUsedMemoryRecorder;
    private ProfilerRecorder m_gfxReservedMemoryRecorder;
    private ProfilerRecorder m_audioUsedMemoryRecorder;
    private ProfilerRecorder m_audioReservedMemoryRecorder;
    private ProfilerRecorder m_videoUsedMemoryRecorder;
    private ProfilerRecorder m_videoReservedMemoryRecorder;
    private ProfilerRecorder m_profilerUsedMemoryRecorder;
    private ProfilerRecorder m_profilerReservedMemoryRecorder;
    private ProfilerRecorder m_systemUsedMemoryRecorder;
    private ProfilerRecorder m_textureCountRecorder;
    private ProfilerRecorder m_textureMemoryRecorder;
    private ProfilerRecorder m_meshCountRecorder;
    private ProfilerRecorder m_meshMemoryRecorder;
    private ProfilerRecorder m_materialCountRecorder;
    private ProfilerRecorder m_materialMemoryRecorder;
    private ProfilerRecorder m_animationClipCountRecorder;
    private ProfilerRecorder m_animationClipMemoryRecorder;
    private ProfilerRecorder m_assetCountRecorder;
    private ProfilerRecorder m_gameObjectsInScenesRecorder;
    private ProfilerRecorder m_totalObjectsInScenesRecorder;
    private ProfilerRecorder m_totalUnityObjectCountRecorder;
    private ProfilerRecorder m_gcAllocationInFrameCountRecorder;
    private ProfilerRecorder m_gcAllocatedInFrameRecorder;

    private void OnEnable()
    {
        m_totalUsedMemoryRecorder          = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Total Used Memory" );
        m_totalReservedMemoryRecorder      = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Total Reserved Memory" );
        m_gcUsedMemoryRecorder             = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "GC Used Memory" );
        m_gcReservedMemoryRecorder         = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "GC Reserved Memory" );
        m_gfxUsedMemoryRecorder            = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Gfx Used Memory" );
        m_gfxReservedMemoryRecorder        = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Gfx Reserved Memory" );
        m_audioUsedMemoryRecorder          = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Audio Used Memory" );
        m_audioReservedMemoryRecorder      = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Audio Reserved Memory" );
        m_videoUsedMemoryRecorder          = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Video Used Memory" );
        m_videoReservedMemoryRecorder      = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Video Reserved Memory" );
        m_profilerUsedMemoryRecorder       = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Profiler Used Memory" );
        m_profilerReservedMemoryRecorder   = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Profiler Reserved Memory" );
        m_systemUsedMemoryRecorder         = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "System Used Memory" );
        m_textureCountRecorder             = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Texture Count" );
        m_textureMemoryRecorder            = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Texture Memory" );
        m_meshCountRecorder                = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Mesh Count" );
        m_meshMemoryRecorder               = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Mesh Memory" );
        m_materialCountRecorder            = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Material Count" );
        m_materialMemoryRecorder           = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Material Memory" );
        m_animationClipCountRecorder       = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "AnimationClip Count" );
        m_animationClipMemoryRecorder      = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "AnimationClip Memory" );
        m_assetCountRecorder               = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Asset Count" );
        m_gameObjectsInScenesRecorder      = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "GameObject Count" );
        m_totalObjectsInScenesRecorder     = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Scene Object Count" );
        m_totalUnityObjectCountRecorder    = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "Object Count" );
        m_gcAllocationInFrameCountRecorder = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "GC Allocation In Frame Count" );
        m_gcAllocatedInFrameRecorder       = ProfilerRecorder.StartNew( ProfilerCategory.Memory, "GC Allocated In Frame" );
    }

    private void OnDisable()
    {
        m_totalUsedMemoryRecorder.Dispose();
        m_totalReservedMemoryRecorder.Dispose();
        m_gcUsedMemoryRecorder.Dispose();
        m_gcReservedMemoryRecorder.Dispose();
        m_gfxUsedMemoryRecorder.Dispose();
        m_gfxReservedMemoryRecorder.Dispose();
        m_audioUsedMemoryRecorder.Dispose();
        m_audioReservedMemoryRecorder.Dispose();
        m_videoUsedMemoryRecorder.Dispose();
        m_videoReservedMemoryRecorder.Dispose();
        m_profilerUsedMemoryRecorder.Dispose();
        m_profilerReservedMemoryRecorder.Dispose();
        m_systemUsedMemoryRecorder.Dispose();
    }

    private void Update()
    {
        var sb = new StringBuilder();
        sb.AppendLine( $"Total Used Memory: {m_systemUsedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Total Reserved Memory: {m_totalReservedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"GC Used Memory: {m_gcUsedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"GC Reserved Memory: {m_gcReservedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Gfx Used Memory: {m_gfxUsedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Gfx Reserved Memory: {m_gfxReservedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Audio Used Memory: {m_audioUsedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Audio Reserved Memory: {m_audioReservedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Video Used Memory: {m_videoUsedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Video Reserved Memory: {m_videoReservedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Profiler Used Memory: {m_profilerUsedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Profiler Reserved Memory: {m_profilerReservedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"System Used Memory: {m_systemUsedMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Texture Count: {m_textureCountRecorder.LastValue}" );
        sb.AppendLine( $"Texture Memory: {m_textureMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Mesh Count: {m_meshCountRecorder.LastValue}" );
        sb.AppendLine( $"Mesh Memory: {m_meshMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Material Count: {m_materialCountRecorder.LastValue}" );
        sb.AppendLine( $"Material Memory: {m_materialMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"AnimationClip Count: {m_animationClipCountRecorder.LastValue}" );
        sb.AppendLine( $"AnimationClip Memory: {m_animationClipMemoryRecorder.LastValue / 1024 / 1024} MB" );
        sb.AppendLine( $"Asset Count: {m_assetCountRecorder.LastValue}" );
        sb.AppendLine( $"GameObject Count: {m_gameObjectsInScenesRecorder.LastValue}" );
        sb.AppendLine( $"Scene Object Count: {m_totalObjectsInScenesRecorder.LastValue}" );
        sb.AppendLine( $"Object Count: {m_totalUnityObjectCountRecorder.LastValue}" );
        sb.AppendLine( $"GC Allocation In Frame Count: {m_gcAllocationInFrameCountRecorder.LastValue}" );
        sb.AppendLine( $"GC Allocated In Frame: {m_gcAllocatedInFrameRecorder.LastValue / 1024 / 1024} MB" );
        m_text = sb.ToString();
    }

    private void OnGUI()
    {
        GUILayout.TextArea( m_text );
    }
}

メモ

  • いくつかの項目(Mesh Memory や Material Memory)は値を取得できなかった
    • 取得方法が間違っている?
    • ProfilerRecorder.Valid が false になっていた
  • 現在は取得したい情報を文字列で指定する必要があるが
    Unity Forum には定数化を考えている旨の記載があった
    • Unity 2020.2 正式リリース時は定数で参照できるようになるかも

参考サイト様