using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEditor;
|
using UnityEngine.Profiling;
|
using UnityEngine.UI;
|
|
public class FPSDisplay : MonoBehaviour
|
{
|
/// <summary>
|
/// 用于显示FPS数据的Text
|
/// </summary>
|
public Text fpsText;
|
|
// 帧率计算频率
|
private const float calcRate = 0.5f;
|
// 本次计算频率下帧数
|
private int frameCount = 0;
|
// 频率时长
|
private float rateDuration = 0f;
|
// 显示帧率
|
private int fps = 0;
|
|
protected bool fpsDisplay = false;
|
|
void Start()
|
{
|
this.frameCount = 0;
|
this.rateDuration = 0f;
|
this.fps = 0;
|
}
|
|
void Update()
|
{
|
++this.frameCount;
|
this.rateDuration += Time.deltaTime;
|
if (this.rateDuration > calcRate)
|
{
|
// 计算帧率
|
this.fps = (int)(this.frameCount / this.rateDuration);
|
this.frameCount = 0;
|
this.rateDuration = 0f;
|
}
|
|
if (UnityEngine.Input.GetKeyDown(KeyCode.P))
|
fpsDisplay = !fpsDisplay;
|
}
|
|
void OnGUI()
|
{
|
GUIStyle bb = new GUIStyle();
|
bb.normal.background = null; //这是设置背景填充的
|
//bb.normal.textColor = new Color(1, 1, 1,1); //设置字体颜色的
|
bb.fontSize = 30; //当然,这是字体颜色
|
|
if (fpsDisplay)
|
bb.normal.textColor = new Color(1, 1, 1, 1); //设置字体颜色的
|
else
|
bb.normal.textColor = new Color(1, 1, 1, 0);
|
|
string disStr = "";
|
if (fps > 0)
|
disStr += ("FPS:" + this.fps.ToString() + "\n");
|
|
if (fpsText)
|
fpsText.text = disStr;
|
else
|
GUI.Label(new Rect(20, 20, 300, 200), disStr,bb);
|
}
|
|
float ByteToM(long byteCount)
|
{
|
return (float)(byteCount / (1024.0f * 1024.0f));
|
}
|
}
|