コガネブログ

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

【Unity】計測した処理時間を Chrome でプロファイリングできるようにするパッケージ「Kogane Complete Events Tracer」を GitHub に公開しました

リポジトリ

使用例

using System.IO;
using System.Threading.Tasks;
using Kogane.CompleteEventsTracer;
using UnityEngine;

public sealed class Example : MonoBehaviour
{
    private async void Start()
    {
        Debug.Log( "開始" );

        var tracer = new CompleteEventsTracer();

        async Task Process( string name, float duration )
        {
            using ( tracer.Start( name ) )
            {
                await Task.Delay( ( int ) ( duration * 1000 ) );
            }
        }

        await Process( "A", 0.1f );
        await Process( "B", 0.2f );
        await Process( "C", 0.3f );

        await Task.WhenAll
        (
            Process( "D", 0.3f ),
            Process( "E", 0.2f ),
            Process( "F", 0.1f )
        );

        await Process( "G", 0.1f );

        var json = tracer.ToJson();

        await File.WriteAllTextAsync( "result.json", json );

        Debug.Log( "完了" );
    }
}

たとえば上記のように CompleteEventsTracer クラスを使用して処理時間を計測し、
その結果を JSON で result.json に出力します

Chrome のアドレスバーに chrome://tracing と入力して
表示されたページで「Load」を押して出力した result.json を選択します

これで計測した処理時間を Chrome 上でプロファイリングできます