zoukankan      html  css  js  c++  java
  • Unity 基于OnGUI显示实时FPS

    FPS(Frame rate)

      用于测量显示帧数的量度。所谓的测量单位为每秒显示帧数(Frames per Second,简称:FPS)或“赫兹”(Hz),通俗来讲就是指动画或视频的画面数。FPS 是测量用于保存、显示动态视频的信息数量。每秒钟帧数越多,所显示的动作就会越流畅。通常,要避免动作不流畅的最低标准是 30。由于人类眼睛的特殊生理结构,如果所看画面之帧率高于16的时候,就会认为是连贯的,此现象称之为视觉暂留。而当刷新率太低时我们肉眼都能感觉到屏幕的闪烁,不连贯,对图像显示效果和视觉感观产生不好的影响。

      电影以每秒24张画面的速度播放,也就是一秒钟内在屏幕上连续投射出24张静止画面。有关动画播放速度的单位是fps,其中的f就是英文单词 Frame(画面、帧),p 就是 Per(每),s 就是 Second(秒)。用中文表达就是多少帧每秒,或每秒多少帧。电影是24fps,通常简称为24帧。

    常见媒体的FPS帧率

    电影:24fps

    电视(PAL):25fps

    电视(NTSL):30fps

    CRT显示器:75Hz以上

    液晶显示器:一般为60Hz

    在游戏过程中一般人不觉得卡顿的FPS频率大约是30Hz,想要达到流畅等级则需要60Hz

    OnGUI显示实时FPS

     1 using System.Collections;
     2 using UnityEngine;
     3 
     4 /// <summary>
     5 /// OnGUI实时显示PFS
     6 /// </summary>
     7 public class FPSOnGUI : MonoBehaviour
     8 {
     9     //fps 显示的初始位置和大小
    10     public Rect startRect = new Rect(512, 10f, 75f, 50f);
    11     //fps 过低时是否改变UI颜色
    12     public bool updateColor = true;
    13     //fps UI 是否允许拖动 
    14     public bool allowDrag = true;
    15     //fps 更新的频率
    16     public float frequency = 0.5F;
    17     //fps 显示的精度
    18     public int nbDecimal = 1;
    19     //一定时间内的fps数量
    20     private float accum = 0f;
    21     //fps计算的时间
    22     private int frames = 0;
    23     //GUI 依赖fps的颜色 fps<10 红色 fps<30 黄色 fps>=30 绿色
    24     private Color color = Color.white;
    25     //fps 
    26     private string sFPS = "";
    27     //GUI 的样式
    28     private GUIStyle style;
    29 
    30     void Start()
    31     {
    32         StartCoroutine(FPS());
    33     }
    34 
    35     void Update()
    36     {
    37         accum += Time.timeScale / Time.deltaTime;
    38         ++frames;
    39     }
    40 
    41     IEnumerator FPS()
    42     {
    43         while (true)
    44         {
    45             //更新fps
    46             float fps = accum / frames;
    47             sFPS = fps.ToString("f" + Mathf.Clamp(nbDecimal, 0, 10));
    48 
    49             //更新颜色
    50             color = (fps >= 30) ? Color.green : ((fps > 10) ? Color.yellow : Color.red);
    51 
    52             accum = 0.0F;
    53             frames = 0;
    54 
    55             yield return new WaitForSeconds(frequency);
    56         }
    57     }
    58 
    59     void OnGUI()
    60     {
    61         if (style == null)
    62         {
    63             style = new GUIStyle(GUI.skin.label);
    64             style.normal.textColor = Color.white;
    65             style.alignment = TextAnchor.MiddleCenter;
    66         }
    67 
    68         GUI.color = updateColor ? color : Color.white;
    69         startRect = GUI.Window(0, startRect, DoMyWindow, "");
    70     }
    71 
    72     //Window窗口
    73     void DoMyWindow(int windowID)
    74     {
    75         GUI.Label(new Rect(0, 0, startRect.width, startRect.height), sFPS + " FPS", style);
    76         if (allowDrag) GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
    77     }
    78 
    79 }

    显示效果

    ***| 以上内容仅为学习参考、学习笔记使用 |***

    原文:https://blog.csdn.net/m0_37998140/article/details/78255799

  • 相关阅读:
    Android 摇一摇之双甩功能
    Android 上千张图片的列表滑动加载
    Android 新手引导
    Android 自定义列表指示器
    Mininet的安装与卸载
    ubuntu装机必备
    linux系统中利用vagrant创建虚拟开发环境
    Ubuntu右键添加:open in terminal
    ryu启动问题总结
    新建WORD文档打开会出现转换文件对话框3步解决办法
  • 原文地址:https://www.cnblogs.com/ChenZiRong1999/p/13098257.html
Copyright © 2011-2022 走看看