該樓層疑似違規已被系統折疊 隐藏此樓檢視此樓
方法一:
[csharp] view plain copy print?
public class DeltaFPS
{
private float _UpdateInterval = 0.1f;//更新周期
private float _Accum;
private int _Frames;
private float _Timeleft;
private string _Fps; //幀率
public void Init()
{
Reset();
}
private void Reset()
{
_Timeleft = updateInterval;
_Accum = 0.0f;
+Frames = 0;
}
public void Update()
{
_Timeleft -= Time.deltaTime;
_Accum += Time.timeScale / Time.deltaTime;
++_Frames;
if (_Timeleft <= 0)
{
_Fps = (_Accum / _Frames).ToString("f2");
Reset();
}
}
public void OnGUI()
{
GUILayout.Label(_Fps);
}
}
方法二:
[csharp] view plain copy print?
public class FPS
{
public float _UpdateInterval = 0.1f;
private float _LastInterval;
private int _Frames = 0;
private float _FPS;
public void Init()
{
_LastInterval = Time.realtimeSinceStartup;
_Frames = 0;
}
public void Update()
{
_Frames++;
if(Time.realtimeSinceStartup > _LastInterval + _UpdateInterval)
{
_FPS = _Frames / (Time.realtimeSinceStartup - _LastInterval);
_Frames = 0;
_LastInterval = Time.realtimeSinceStartup;
}
}
public void OnGUI()
{
GUILayout.Label(_FPS.ToString());
}
}
在自己的腳本中new一個DeltaFPS或者FPS類:fps[csharp] view plain copy print?
private FPS fps;
[csharp] view plain copy print?
void Start () {
fps = new FPS();
fps.Init();
}
[csharp] view plain copy print?
void Update()
{
fps.Update();
}
[csharp] view plain copy print?
void OnGUI()
{
fps.OnGUI();
}
這樣在遊戲中就可以看到目前幀率了。