Unity Runtime 性能指标查看

Unity Runtime 性能指标查看

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

[RequireComponent(typeof(Text))]
public class FPSCounter : MonoBehaviour
{
    const float fpsMeasurePeriod = 0.5f;
    int m_FpsAccumulator;
    float m_FpsNextPeriod;
    int m_CurrentFps;
    string display = "{0} FPS";
    Text m_Text;

    void Start()
    {
        m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
        m_Text = GetComponent<Text>();
        m_Text.text = display;
    }


    ProfilerRecorder setPassCallsRecorder;
    ProfilerRecorder drawCallsRecorder;
    ProfilerRecorder verticesRecorder;
    ProfilerRecorder trianglesRecorder;

    ProfilerRecorder totalReservedMemoryRecorder;
    ProfilerRecorder gcReservedMemoryRecorder;
    ProfilerRecorder systemUsedMemoryRecorder;
    ProfilerRecorder textureMemoryMemoryRecorder;

    void OnEnable()
    {
        setPassCallsRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Render, "SetPass Calls Count");
        drawCallsRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Render, "Draw Calls Count");
        verticesRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Render, "Vertices Count");
        trianglesRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Render, "Triangles Count");

        totalReservedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Total Reserved Memory");
        gcReservedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Reserved Memory");
        systemUsedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "System Used Memory");
        textureMemoryMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Texture Memory");

    }

    void OnDisable()
    {
        setPassCallsRecorder.Dispose();
        drawCallsRecorder.Dispose();
        verticesRecorder.Dispose();
        trianglesRecorder.Dispose();

        totalReservedMemoryRecorder.Dispose();
        gcReservedMemoryRecorder.Dispose();
        systemUsedMemoryRecorder.Dispose();

        textureMemoryMemoryRecorder.Dispose();
    }


    void Update()
    {

        var sb = new StringBuilder(500);
        m_FpsAccumulator++;
        if (Time.realtimeSinceStartup > m_FpsNextPeriod)
        {
            m_CurrentFps = (int)(m_FpsAccumulator / fpsMeasurePeriod);
            m_FpsAccumulator = 0;
            m_FpsNextPeriod += fpsMeasurePeriod;
            sb.AppendLine(string.Format(display, m_CurrentFps));

            if (setPassCallsRecorder.Valid)
                sb.AppendLine($"SetPass Calls: {setPassCallsRecorder.LastValue}");
            if (drawCallsRecorder.Valid)
                sb.AppendLine($"Draw Calls: {drawCallsRecorder.LastValue}");
            if (verticesRecorder.Valid)
                sb.AppendLine($"Vertices: {verticesRecorder.LastValue}");
            if (trianglesRecorder.Valid)
                sb.AppendLine($"Triangles: {trianglesRecorder.LastValue}");

            if (totalReservedMemoryRecorder.Valid)
                sb.AppendLine($"总保留内存: {(totalReservedMemoryRecorder.LastValue / 1024f) / 1024f} MB");
            if (gcReservedMemoryRecorder.Valid)
                sb.AppendLine($"GC保留内存: {(gcReservedMemoryRecorder.LastValue / 1024f) / 1024f} MB");
            if (systemUsedMemoryRecorder.Valid)
                sb.AppendLine($"系统已用内存: {(systemUsedMemoryRecorder.LastValue / 1024f) / 1024f} MB");
            if (textureMemoryMemoryRecorder.Valid)
                sb.AppendLine($"贴图占用内存: {(textureMemoryMemoryRecorder.LastValue / 1024f) / 1024f} MB");


            m_Text.text = sb.ToString();
        }
        sb.Clear();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值