查看: 723|回复: 0
打印 上一主题 下一主题

[其他] unity中UI界面显示FPS

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53464
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2016-3-7 22:38:40 |只看该作者 |倒序浏览
  1. using UnityEngine;
  2. using System.Collections;

  3. public class HUDFps : MonoBehaviour
  4. {

  5.     // Attach this to a GUIText to make a frames/second indicator.
  6.     //
  7.     // It calculates frames/second over each updateInterval,
  8.     // so the display does not keep changing wildly.
  9.     //
  10.     // It is also fairly accurate at very low FPS counts (<10).
  11.     // We do this not by simply counting frames per interval, but
  12.     // by accumulating FPS for each frame. This way we end up with
  13.     // correct overall FPS even if the interval renders something like
  14.     // 5.5 frames.

  15.     public float updateInterval = 0.5F;

  16.     private float accum = 0; // FPS accumulated over the interval
  17.     private int frames = 0; // Frames drawn over the interval
  18.     private float timeleft; // Left time for current interval
  19.     public UnityWebView meshRender;

  20.     void Start()
  21.     {
  22.         if (!GetComponent<GUIText>())
  23.         {
  24.             Debug.Log("UtilityFramesPerSecond needs a GUIText component!");
  25.             enabled = false;
  26.             return;
  27.         }
  28.         timeleft = updateInterval;
  29.     }

  30.     void Update()
  31.     {
  32.         timeleft -= Time.deltaTime;
  33.         accum += Time.timeScale / Time.deltaTime;
  34.         ++frames;

  35.         // Interval ended - update GUI text and start new interval
  36.         if (timeleft <= 0.0)
  37.         {
  38.             // display two fractional digits (f2 format)
  39.             float fps = accum / frames;
  40.             string format = System.String.Format("{0:F2} FPS", fps);
  41.             GetComponent<GUIText>().text = format;

  42.             if (fps < 30)
  43.                 GetComponent<GUIText>().material.color = Color.yellow;
  44.             else
  45.                 if (fps < 10)
  46.                     GetComponent<GUIText>().material.color = Color.red;
  47.                 else
  48.                     GetComponent<GUIText>().material.color = Color.green;

  49.             meshRender.setFPS(fps);
  50.             //  DebugConsole.Log(format,level);
  51.             timeleft = updateInterval;
  52.             accum = 0.0F;
  53.             frames = 0;
  54.         }
  55.     }
  56. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-17 12:52 , Processed in 0.077714 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部