Files
TaiWan/Assets/Roaming/Scripts/Widget/FPSOnGUIText.cs
2025-10-31 15:20:38 +08:00

56 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// FPS 显示于OnGUI
/// </summary>
public class FPSOnGUIText : MonoBehaviour
{
float updateInterval = 1.0f; //当前时间间隔
private float accumulated = 0.0f; //在此期间累积
private float frames = 0; //在间隔内绘制的帧
private float timeRemaining; //当前间隔的剩余时间
private float fps = 15.0f; //当前帧 Current FPS
private float lastSample;
void Start()
{
//if (!H5Controller.IsShowFPS())
// Destroy(this);
DontDestroyOnLoad(this.gameObject); //不销毁此游戏对象,在哪个场景都可以显示,不需要则注释
timeRemaining = updateInterval;
lastSample = Time.realtimeSinceStartup; //实时自启动
}
void Update()
{
++frames;
float newSample = Time.realtimeSinceStartup;
float deltaTime = newSample - lastSample;
lastSample = newSample;
timeRemaining -= deltaTime;
accumulated += 1.0f / deltaTime;
if (timeRemaining <= 0.0f)
{
fps = accumulated / frames;
timeRemaining = updateInterval;
accumulated = 0.0f;
frames = 0;
}
}
void OnGUI()
{
GUIStyle style = new GUIStyle
{
border = new RectOffset(10, 10, 10, 10),
fontSize = 50,
fontStyle = FontStyle.BoldAndItalic,
};
//自定义宽度 ,高度大小 颜色style
GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height - 100, 200, 200), "<color=#00ff00><size=30>" + "FPS:" + fps.ToString("f2") + "</size></color>", style);
}
}