Efecto
Este artículo proporciona dos métodos de presentación FPS,
la pantalla GUI blanca en la esquina superior izquierda
y nuestra pantalla TextMeshPro.
Debido a que la versión utilizada en este programa es la versión 2021, se utiliza TextMeshPro.
El siguiente script utilizará la versión TextMeshpro de 2021 versión y la versión de texto de otras versiones de UGUI.
Se presenta la parte roja del FPS
El primer script TextMeshPro
Cree un script FPS, haga el siguiente script
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 打印FPS
/// </summary>
public class FPS : MonoBehaviour
{
float _updateInterval = 1f;//设定更新帧率的时间间隔为1秒
float _accum = .0f;//累积时间
int _frames = 0;//在_updateInterval时间内运行了多少帧
float _timeLeft;
string fpsFormat;
public TextMeshProUGUI text;
void Start()
{
_timeLeft = _updateInterval;
}
void Update()
{
_timeLeft -= Time.deltaTime;
//Time.timeScale可以控制Update 和LateUpdate 的执行速度,
//Time.deltaTime是以秒计算,完成最后一帧的时间
//相除即可得到相应的一帧所用的时间
_accum += Time.timeScale / Time.deltaTime;
++_frames;//帧数
if (_timeLeft <= 0)
{
float fps = _accum / _frames;
//Debug.Log(_accum + "__" + _frames);
fpsFormat = System.String.Format("{0:F2}FPS", fps);//保留两位小数
// Debug.LogError(fpsFormat);
_timeLeft = _updateInterval;
_accum = .0f;
_frames = 0;
}
text.text = fpsFormat;
}
}
Luego, puede asignar activamente el texto que debe mostrarse en Unity
El segundo texto
La siguiente es la versión de texto debajo de la versión 2021
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 打印FPS
/// </summary>
public class FPS : MonoBehaviour
{
float _updateInterval = 1f;//设定更新帧率的时间间隔为1秒
float _accum = .0f;//累积时间
int _frames = 0;//在_updateInterval时间内运行了多少帧
float _timeLeft;
string fpsFormat;
public Text text;
void Start()
{
_timeLeft = _updateInterval;
}
void Update()
{
_timeLeft -= Time.deltaTime;
//Time.timeScale可以控制Update 和LateUpdate 的执行速度,
//Time.deltaTime是以秒计算,完成最后一帧的时间
//相除即可得到相应的一帧所用的时间
_accum += Time.timeScale / Time.deltaTime;
++_frames;//帧数
if (_timeLeft <= 0)
{
float fps = _accum / _frames;
//Debug.Log(_accum + "__" + _frames);
fpsFormat = System.String.Format("{0:F2}FPS", fps);//保留两位小数
// Debug.LogError(fpsFormat);
_timeLeft = _updateInterval;
_accum = .0f;
_frames = 0;
}
text.text = fpsFormat;
}
}
Versión FPS blanca
La diferencia con otras versiones es que el script se puede usar directamente sin asignación, y la diferencia es solo un método más de OnGUi,
el punto es que el script se puede usar sin importar la versión.
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 打印FPS
/// </summary>
public class FPS : MonoBehaviour
{
float _updateInterval = 1f;//设定更新帧率的时间间隔为1秒
float _accum = .0f;//累积时间
int _frames = 0;//在_updateInterval时间内运行了多少帧
float _timeLeft;
string fpsFormat;
void Start()
{
_timeLeft = _updateInterval;
}
void OnGUI()
{
GUI.Label(new Rect(100, 100, 200, 200), fpsFormat);
}
void Update()
{
_timeLeft -= Time.deltaTime;
//Time.timeScale可以控制Update 和LateUpdate 的执行速度,
//Time.deltaTime是以秒计算,完成最后一帧的时间
//相除即可得到相应的一帧所用的时间
_accum += Time.timeScale / Time.deltaTime;
++_frames;//帧数
if (_timeLeft <= 0)
{
float fps = _accum / _frames;
//Debug.Log(_accum + "__" + _frames);
fpsFormat = System.String.Format("{0:F2}FPS", fps);//保留两位小数
// Debug.LogError(fpsFormat);
_timeLeft = _updateInterval;
_accum = .0f;
_frames = 0;
}
}
}