Unity3D基于OnGUI實時顯示FPS
幀率(Frame rate)是用于測量顯示幀數的量度。所謂的測量單位為每秒顯示幀數(Frames per Second,簡稱:FPS)或“赫茲”(Hz)。此詞多用于影視制作和電子游戲。由于人類眼睛的特殊生理結構,如果所看畫面之幀率高于16的時候,就會認為是連貫的,此現象稱之為視覺暫留。
每秒的幀數(fps)或者說幀率表示圖形處理器處理場時每秒鐘能夠更新的次數。高的幀率可以得到更流暢、更逼真的動畫。一般來說30fps就是可以接受的,但是將性能提升至60fps則可以明顯提升交互感和逼真感,但是一般來說超過75fps一般就不容易察覺到有明顯的流暢度提升了。如果幀率超過屏幕刷新率只會浪費圖形處理的能力,因為監(jiān)視器不能以這么快的速度更新,這樣超過刷新率的幀率就浪費掉了。
以下是在Unity3D中顯示fps的代碼。
using UnityEngine;
using System.Collections;
 
[AddComponentMenu( "Utilities/HUDFPS")]
public class FPSCounter : MonoBehaviour
{
 //fps 顯示的初始位置和大小
 public Rect startRect=new Rect(512, 10f, 75f, 50f );
 //fps 過低時是否改變UI顏色
 public bool updateColor = true;
 //fps UI 是否允許拖動 
 public bool allowDrag = true; 
 //fps 更新的頻率
 public float frequency = 0.5F;
 //fps 顯示的精度
 public int nbDecimal = 1; 
 //一定時間內的fps數量
 private float accum = 0f;
 //fps計算的時間
 private int frames = 0;
 //GUI 依賴fps的顏色 fps<10 紅色 fps<30 黃色 fps>=30 綠色
 private Color color = Color.white;
 //fps 
 private string sFPS = "";
 //GUI 的樣式
 private GUIStyle style;
 
 void Start()
 {
 StartCoroutine(FPS());
 }
 
 void Update()
 {
 accum += Time.timeScale/ Time.deltaTime;
 ++frames;
 }
 
 IEnumerator FPS()
 {
 while( true )
 {
 //更新fps
 float fps = accum/frames;
 sFPS = fps.ToString( "f" + Mathf.Clamp( nbDecimal, 0, 10 ) );
 
 //更新顏色
 color = (fps >= 30) ? Color.green : ((fps > 10) ? Color.yellow : Color.red);
 
 accum = 0.0F;
 frames = 0;
 
 yield return new WaitForSeconds( frequency );
 }
 }
 
 void OnGUI()
 {
 if( style == null ){
 style = new GUIStyle( GUI.skin.label );
 style.normal.textColor = Color.white;
 style.alignment = TextAnchor.MiddleCenter;
 }
 
 GUI.color = updateColor ? color : Color.white;
 startRect = GUI.Window(0, startRect, DoMyWindow, "");
 }
 
 void DoMyWindow(int windowID)
 {
 GUI.Label( new Rect(0, 0, startRect.width, startRect.height), sFPS + " FPS", style );
 if( allowDrag ) GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
 }
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 01-10基于C#實現簡單離線注冊碼生成與驗證
 - 01-10C#基于UDP實現的P2P語音聊天工具
 - 01-10C#實現基于加減按鈕形式控制系統(tǒng)音量及靜音的方法
 - 01-10C#基于WebBrowser獲取cookie的實現方法
 - 01-10C#基于委托實現多線程之間操作的方法
 - 01-10Unity3d獲取系統(tǒng)時間
 - 01-10Unity3D獲取當前鍵盤按鍵及Unity3D鼠標、鍵盤的基本操作
 - 01-10基于C#對用戶密碼使用MD5加密與解密
 - 01-10基于C#實現簡單的隨機抽獎小程序
 - 01-10C#基于cookie實現的購物車功能
 


閱讀排行
本欄相關
- 01-10C#通過反射獲取當前工程中所有窗體并
 - 01-10關于ASP網頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實現txt定位指定行完整實例
 - 01-10WinForm實現仿視頻播放器左下角滾動新
 - 01-10C#停止線程的方法
 - 01-10C#實現清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實現讀取注冊表監(jiān)控當前操作系統(tǒng)已
 
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
 - 08-05dedecms(織夢)副欄目數量限制代碼修改
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 01-10C#中split用法實例總結
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10SublimeText編譯C開發(fā)環(huán)境設置
 - 01-10delphi制作wav文件的方法
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-11ajax實現頁面的局部加載
 - 04-02jquery與jsp,用jquery
 


