Files
zzwater/Assets/Others/FPS.cs
2024-07-17 12:22:50 +08:00

51 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPS : MonoBehaviour
{
public static bool showFPS = true;
public static float fps;
private static float frameCount;
private static float elapse;
private GUIStyle _fpsStyle;
public GUIStyle fpsStyle
{
get
{
if (_fpsStyle == null)
{
int h = Screen.height;
_fpsStyle = new GUIStyle();
_fpsStyle.alignment = TextAnchor.UpperLeft;
_fpsStyle.fontSize = h * 2 / 100;
_fpsStyle.normal.textColor = Color.magenta;
}
return _fpsStyle;
}
}
void Start()
{
Application.targetFrameRate = 0;
}
void OnGUI()
{
frameCount++;
elapse += Time.unscaledDeltaTime;
if (elapse >= 1)
{
fps = frameCount / elapse;
elapse = 0;
frameCount = 0;
}
if (!showFPS) return;
string text = string.Format("FPS: {0:0.}", fps);
int w = Screen.width, h = Screen.height;
Rect rect = new Rect(20, h * 0.4f, w * 0.5f, h * 2 / 100);
GUI.Label(rect, text, fpsStyle);
}
}