Unity3D基于OnGUI實(shí)時(shí)顯示FPS
幀率(Frame rate)是用于測(cè)量顯示幀數(shù)的量度。所謂的測(cè)量單位為每秒顯示幀數(shù)(Frames per Second,簡(jiǎn)稱:FPS)或“赫茲”(Hz)。此詞多用于影視制作和電子游戲。由于人類眼睛的特殊生理結(jié)構(gòu),如果所看畫面之幀率高于16的時(shí)候,就會(huì)認(rèn)為是連貫的,此現(xiàn)象稱之為視覺(jué)暫留。
每秒的幀數(shù)(fps)或者說(shuō)幀率表示圖形處理器處理場(chǎng)時(shí)每秒鐘能夠更新的次數(shù)。高的幀率可以得到更流暢、更逼真的動(dòng)畫。一般來(lái)說(shuō)30fps就是可以接受的,但是將性能提升至60fps則可以明顯提升交互感和逼真感,但是一般來(lái)說(shuō)超過(guò)75fps一般就不容易察覺(jué)到有明顯的流暢度提升了。如果幀率超過(guò)屏幕刷新率只會(huì)浪費(fèi)圖形處理的能力,因?yàn)楸O(jiān)視器不能以這么快的速度更新,這樣超過(guò)刷新率的幀率就浪費(fèi)掉了。
以下是在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 過(guò)低時(shí)是否改變UI顏色 public bool updateColor = true; //fps UI 是否允許拖動(dòng) public bool allowDrag = true; //fps 更新的頻率 public float frequency = 0.5F; //fps 顯示的精度 public int nbDecimal = 1; //一定時(shí)間內(nèi)的fps數(shù)量 private float accum = 0f; //fps計(jì)算的時(shí)間 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)); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#判斷某個(gè)軟件是否已安裝實(shí)現(xiàn)代碼分享
這篇文章主要介紹了C#判斷某個(gè)軟件是否已安裝實(shí)現(xiàn)代碼分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06C#實(shí)現(xiàn)獲取電腦硬件顯卡信息的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)獲取電腦硬件顯卡信息,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01c#求范圍內(nèi)素?cái)?shù)的示例分享(c#求素?cái)?shù))
問(wèn)題是判斷101-200之間有多少個(gè)素?cái)?shù),并輸出所有素?cái)?shù)。下面是使用C#解決這個(gè)問(wèn)題的方法 ,需要的朋友可以參考下2014-03-03C#使用MailAddress類發(fā)送html格式郵件的實(shí)例代碼
這篇文章主要介紹如何使用C#的MailAddress類發(fā)送郵件的方法,大家參考使用吧2013-11-11關(guān)于C#中使用Oracle存儲(chǔ)過(guò)程返回結(jié)果集的問(wèn)題
Oracle中可以使用游標(biāo)(Cursor)對(duì)數(shù)據(jù)集進(jìn)行操作,但在存儲(chǔ)過(guò)程輸出參數(shù)中直接使用Cursor錯(cuò)誤,下面小編給大家?guī)?lái)了C#中使用Oracle存儲(chǔ)過(guò)程返回結(jié)果集的問(wèn)題,感興趣的朋友一起看看吧2021-10-10WPF實(shí)現(xiàn)帶模糊搜索的DataGrid的示例代碼
這篇文章主要為大家詳細(xì)介紹了WPF如何實(shí)現(xiàn)帶模糊搜索的DataGrid,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-02-02