コガネブログ

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

【Unity】Unity 新機能 - スクリプトから SetPass Calls や Draw Calls を参照できるようになった

概要

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

public class Example : MonoBehaviour
{
    private string           m_statsText;
    private ProfilerRecorder m_setPassCallsRecorder;
    private ProfilerRecorder m_drawCallsRecorder;
    private ProfilerRecorder m_trianglesRecorder;
    private ProfilerRecorder m_verticesRecorder;

    private void OnEnable()
    {
        m_setPassCallsRecorder = ProfilerRecorder.StartNew( ProfilerCategory.Render, "SetPass Calls Count" );
        m_drawCallsRecorder    = ProfilerRecorder.StartNew( ProfilerCategory.Render, "Draw Calls Count" );
        m_trianglesRecorder    = ProfilerRecorder.StartNew( ProfilerCategory.Render, "Triangles Count" );
        m_verticesRecorder     = ProfilerRecorder.StartNew( ProfilerCategory.Render, "Vertices Count" );
    }

    private void OnDisable()
    {
        m_setPassCallsRecorder.Dispose();
        m_drawCallsRecorder.Dispose();
        m_trianglesRecorder.Dispose();
        m_verticesRecorder.Dispose();
    }

    private void Update()
    {
        var sb = new StringBuilder( 500 );
        if ( m_setPassCallsRecorder.Valid ) sb.AppendLine( $"SetPass Calls: {m_setPassCallsRecorder.LastValue}" );
        if ( m_drawCallsRecorder.Valid ) sb.AppendLine( $"Draw Calls: {m_drawCallsRecorder.LastValue}" );
        if ( m_trianglesRecorder.Valid ) sb.AppendLine( $"Triangles: {m_trianglesRecorder.LastValue}" );
        if ( m_verticesRecorder.Valid ) sb.AppendLine( $"Vertices: {m_verticesRecorder.LastValue}" );
        m_statsText = sb.ToString();
    }

    private void OnGUI()
    {
        GUI.TextArea( new Rect( 10, 30, 250, 65 ), m_statsText );
    }
}

f:id:baba_s:20210125111146p:plain

参考サイト様

関連記事