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