Files
TaiWan/Assets/Roaming/Scripts/Widget/FPSOnGUIText.cs

56 lines
1.6 KiB
C#
Raw Normal View History

2025-10-31 15:20:38 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// FPS <20><>ʾ<EFBFBD><CABE>OnGUI
/// </summary>
public class FPSOnGUIText : MonoBehaviour
{
float updateInterval = 1.0f; //<2F><>ǰʱ<C7B0><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private float accumulated = 0.0f; //<2F>ڴ<EFBFBD><DAB4>ڼ<EFBFBD><DABC>ۻ<EFBFBD>
private float frames = 0; //<2F>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD>ڻ<EFBFBD><DABB>Ƶ<EFBFBD>֡
private float timeRemaining; //<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3>ʱ<EFBFBD><CAB1>
private float fps = 15.0f; //<2F><>ǰ֡ Current FPS
private float lastSample;
void Start()
{
//if (!H5Controller.IsShowFPS())
// Destroy(this);
DontDestroyOnLoad(this.gameObject); //<2F><><EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD><D9B4><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ע<EFBFBD><D7A2>
timeRemaining = updateInterval;
lastSample = Time.realtimeSinceStartup; //ʵʱ<CAB5><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
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,
};
//<2F>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>߶ȴ<DFB6>С <20><>ɫ<EFBFBD><C9AB>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);
}
}