unity 增加系统时间显示、FPS帧率、ms延迟

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using UnityEngine;

public class Frame : MonoBehaviour
{
   
    // 记录帧数
    private int _frame;
    // 上一次计算帧率的时间
    private float _lastTime;
    // 平均每一帧的时间
    private float _frameDeltaTime;
    // 间隔多长时间(秒)计算一次帧率
    private float _Fps;
    private const float _timeInterval = 0.5f;

    void Start()
    {
   
        _lastTime = Time.realtimeSinceStartup;
    }

    void Update()
    {
   
        FrameCalculate();
    }

    private void FrameCalculate()
    {
   
        _frame++;
        if (Time.realtimeSinceStartup - _lastTime < _timeInterval)
        {
   
            return;
        }

        float time = Time.realtimeSinceStartup - _lastTime;
        _Fps = _frame / time;
        _frameDeltaTime = time / _frame;

        _lastTime = Time.realtimeSinceStartup;
        _frame = 0;
    }

    private void OnGUI()
    {
   
        string msg = string.Format("<color=red><size=30>FPS:{0}   ms:{1}</size></color>",(int) _Fps , (int)(_frameDeltaTime *1000));
        GUI.Label(new Rect(Screen.width-230, 0, 500, 150), msg);

        var timeNow = System.DateTime.Now;
        string time = string.Format("<color=green><size=30>{0}</size></color>", timeNow);
        GUI.Label(new Rect(Screen.width/2-200, 2, 600, 50), time);
    }
}

将脚本挂载到相机上面

在这里插入图片描述

效果

在这里插入图片描述

相关推荐

  1. 限制Unity的方式

    2024-02-07 02:16:01       58 阅读
  2. Unity中控制的思考

    2024-02-07 02:16:01       43 阅读
  3. 视频-数-FPS

    2024-02-07 02:16:01       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-07 02:16:01       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-07 02:16:01       97 阅读
  3. 在Django里面运行非项目文件

    2024-02-07 02:16:01       78 阅读
  4. Python语言-面向对象

    2024-02-07 02:16:01       88 阅读

热门阅读

  1. 关于自动驾驶概念的学习和一些理解

    2024-02-07 02:16:01       52 阅读
  2. CSS选择器及其优先级

    2024-02-07 02:16:01       50 阅读
  3. docker安装gitlab-runner

    2024-02-07 02:16:01       48 阅读
  4. 2.5学习总结9

    2024-02-07 02:16:01       43 阅读
  5. DBA不仅仅是管理数据库--也要管理中间件

    2024-02-07 02:16:01       56 阅读
  6. 记录 | python isinstance()用法

    2024-02-07 02:16:01       62 阅读
  7. 关于RabbitMQ常见的十道面试题

    2024-02-07 02:16:01       48 阅读