using System;
using System.Collections.Generic;
using UnityEngine;
public class Debugger : MonoBehaviour
{
void Start()
{
timeleft = updateInterval;
}
void Update()
{
UpdateUsed();
UpdateFPS();
}
//Memory
private string sUserMemory;
private string s;
public bool OnMemoryGUI;
private uint MonoUsedM;
private uint AllMemory;
[Range(0, 100)]
public int MaxMonoUsedM = 50;
[Range(0, 400)]
public int MaxAllMemory = 200;
void UpdateUsed()
{
sUserMemory = "";
MonoUsedM = Profiler.GetMonoUsedSize() / 1000000;
AllMemory = Profiler.GetTotalAllocatedMemory() / 1000000;
sUserMemory += "MonoUsed:" + MonoUsedM + "M" + "\n";
sUserMemory += "AllMemory:" + AllMemory + "M" + "\n";
sUserMemory += "UnUsedReserved:" + Profiler.GetTotalUnusedReservedMemory() / 1000000 + "M" + "\n";
s = "";
s += " MonoHeap:" + Profiler.GetMonoHeapSize() / 1000 + "k";
s += " MonoUsed:" + Profiler.GetMonoUsedSize() / 1000 + "k";
s += " Allocated:" + Profiler.GetTotalAllocatedMemory() / 1000 + "k";
s += " Reserved:" + Profiler.GetTotalReservedMemory() / 1000 + "k";
s += " UnusedReserved:" + Profiler.GetTotalUnusedReservedMemory() / 1000 + "k";
s += " UsedHeap:" + Profiler.usedHeapSize / 1000 + "k";
}
//FPS
float updateInterval = 0.5f;
private float accum = 0.0f;
private float frames = 0;
private float timeleft;
private float fps;
private string FPSAAA;
[Range(0, 150)]
public int MaxFPS;
void UpdateFPS()
{
timeleft -= Time.deltaTime;
accum += Time.timeScale / Time.deltaTime;
++frames;
if (timeleft <= 0.0)
{
fps = accum / frames;
FPSAAA = "FPS: " + fps.ToString("f2");
timeleft = updateInterval;
accum = 0.0f;
frames = 0;
}
}
void OnGUI()
{
if (OnMemoryGUI)
{
GUI.color = new Color(1, 0, 0);
GUI.Label(new Rect(10, 10, 200, 60), sUserMemory);
GUI.Label(new Rect(10, 60, 100, 30), FPSAAA);
if (MonoUsedM > MaxMonoUsedM)
{
GUI.backgroundColor = new Color(1, 0, 0);
GUI.Button(new Rect(0, 0, 1024, 1024), "MonoUsedM Waming!!内存不足");
}
if (AllMemory > MaxAllMemory)
{
GUI.backgroundColor = new Color(1, 0, 1);
GUI.Button(new Rect(0, 0, 1024, 1024), "AllMemory Waming!!内存堪忧");
}
if (fps > MaxFPS)
{
GUI.backgroundColor = new Color(1, 0.4f, 0.5f);
GUI.Button(new Rect(0, 0, 1024, 1024), "FPS Waming!!");
}
}
}
}
相关文章
- Android获取当前正在运行的线程和使用的内存
- lunix脚本进程挂掉时显示cpu和内存信息及挂掉的时间
- 规范Unity工程目录和脚本结构能有效提升开发效率、降低维护成本
- python脚本监控获取当前Linux操作系统[内存]/[cpu]/[硬盘]/[登录用户]
- Unity3D学习FPS游戏(3)玩家第一人称视角转动和移动
- Android adb方式获取手机总内存和可用内存信息
- java对象的内存布局(二):利用sun.misc.Unsafe获取类字段的偏移地址和读取字段的值
- Unity3D研究院编辑器之脚本获取资源内存和硬盘大小
- Java获取虚拟机内存和操作系统内存及其线程
- Java 获取JVM内存和物理内存信息