Unity FPS 计算

FPS 是一段时间内的平均值。平均 FPS = 帧数 / 一段时长。帧数可以用每次进入 Update 时加一的变量来统计。一段时长就是进入 Update 时 Time.deltaTime 的累加因为是平均值

public class FPSDisplay : MonoBehaviour {

    public float showTime = 1f;
    public Text tvFpsInfo;

    private int m_count = 0;
    private float m_deltaTime = 0f;

    private void Update () {
        m_count++;
        m_deltaTime += Time.deltaTime;
        if (m_deltaTime >= showTime) {
            float fps = m_count / m_deltaTime;
            float ms = m_deltaTime * 1000 / m_count;
            Debug.Log($"{fps} FPS ({ms}ms)");
            m_count = 0;
            m_deltaTime = 0f;
        }
    }
}

优化写法

using UnityEngine;
using System.Collections;

public class FPSDisplay : MonoBehaviour{

	private float m_time = 0.0f;

	void Update(){
		m_time += (Time.unscaledDeltaTime - m_time) * 0.1f;
		
		float ms = m_time * 1000.0f;
		float fps = 1.0f / m_time;
		
		Debug.Log($"{fps} FPS ({ms}ms)");
	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值