Unity 显示帧率的方法


方法一

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);
		}
	}


方法二


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

private FPS fps;

void Start () {
		fps = new FPS();
		fps.Init();
	}

void Update()
	{
		fps.Update();
    }

void OnGUI()
	{
		fps.OnGUI();
	}


这样在游戏中就可以看到当前帧率了。

你可能感兴趣的:(Unity3D随笔)