using System;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.UI;
public class DlgFramesList : MonoBehaviour
{
public Text fpsText;
public Text heapSizeText;
public Text usedSizeText;
public Text allocatedMemoryText;
public Text reservedMemoryText;
public Text unusedReservedMemoryText;
public Text cpuText;
private int _index = 1;
private int _indexCount = 100;
private int _framesIndex;
private float _fps;
private float _curTime;
private float _lastTime;
private float _fpsDelay;
private TimeSpan _preCpuTime;
private TimeSpan _curCpuTime;
private const long Kb = 1024;
private const long Mb = 1024 * 1024;
private void Start()
{
_fpsDelay = 0.5F;
_curCpuTime = Process.GetCurrentProcess().TotalProcessorTime;
_preCpuTime = _curCpuTime;
_curTime = Time.realtimeSinceStartup;
_lastTime = _curTime;
}
private void Update()
{
if (!gameObject.activeSelf)
{
return;
}
_index++;
if (_index == _indexCount)
{
ShowProfilerMsg();
}
_curCpuTime = Process.GetCurrentProcess().TotalProcessorTime;
if ((_curCpuTime - _preCpuTime).TotalMilliseconds >= 1000)
{
ShowCpuMsg();
}
_framesIndex++;
_curTime = Time.realtimeSinceStartup;
if (_curTime - _lastTime > _fpsDelay)
{
ShowFpsMsg();
}
}
private void ShowProfilerMsg()
{
_index = 0;
//堆内存
if (heapSizeText)
{
heapSizeText.text = "堆内存 : " + Profiler.GetMonoHeapSizeLong() / Mb + " Mb";
}
//使用的
if (usedSizeText)
{
usedSizeText.text = "使用大小 : " + Profiler.GetMonoUsedSizeLong() / Mb + " Mb";
}
// unity分配
if (allocatedMemoryText)
{
allocatedMemoryText.text = "Unity分配 : " + Profiler.GetTotalAllocatedMemoryLong() / Mb + " Mb";
}
// 总内存
if (reservedMemoryText)
{
reservedMemoryText.text = "总内存 : " + Profiler.GetTotalReservedMemoryLong() / Mb + " Mb";
}
// 未使用内存
if (unusedReservedMemoryText)
{
unusedReservedMemoryText.text = "未使用内存 : " + Profiler.GetTotalUnusedReservedMemoryLong() / Mb + " Mb";
}
}
private void ShowCpuMsg()
{
// 间隔时间(毫秒)
int interval = 1000;
var value = (_curCpuTime - _preCpuTime).TotalMilliseconds / interval / Environment.ProcessorCount * 100;
_preCpuTime = _curCpuTime;
if (cpuText)
{
cpuText.text = "CpuValue : " + value.ToString("f2");
}
}文章来源:https://www.toymoban.com/news/detail-764794.html
private void ShowFpsMsg()
{
_fps = _framesIndex / (_curTime - _lastTime);
_framesIndex = 0;
_lastTime = _curTime;
if (fpsText)
{
fpsText.text = "FPS : " + _fps.ToString("f2");
}
}
}文章来源地址https://www.toymoban.com/news/detail-764794.html
到了这里,关于查看Unity当前Fps和内存的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!