Unity3D-计算帧率FPS

版权声明: https://blog.csdn.net/IT_yanghui/article/details/83149326

 网上有很多计算FPS的方法,一般计算没有达到百分之百准确的帧率,只有更接近实际帧率的计算方式。

下面是本人测试多种方法之后觉得比较接近实际帧率的计算方式。

public class FPS : MonoBehaviour
    {
        [SerializeField]
        private Text showFPSText;
        private float fpsByDeltatime = 1.5f;
        private float passedTime = 0.0f;
        private int frameCount = 0;
        private float realtimeFPS = 0.0f;
        void Start()
        {
            SetFPS();
        }
        void Update()
        {
            GetFPS();
        }
        private void SetFPS()
        {
            //如果QualitySettings.vSyncCount属性设置,这个值将被忽略。
            //设置应用平台目标帧率为 60
            Application.targetFrameRate = 60;
        }
        private void GetFPS()
        {
            if (showFPSText == null) return;

            //第一种方式获取FPS
            //float fps = 1.0f / Time.smoothDeltaTime;
            //showFPSText.text = "FPS:  " + fps.ToString();

            //第二种方式
            frameCount++;
            passedTime += Time.deltaTime;
            if(passedTime >= fpsByDeltatime)
            {
                realtimeFPS = frameCount / passedTime;
                showFPSText.text = "FPS:  " + realtimeFPS.ToString("f1");
                passedTime = 0.0f;
                frameCount = 0;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/IT_yanghui/article/details/83149326