在Unity3d 中能夠通過代碼設置 來限定游戲幀率。
Application.targetFrameRate=-1;
設置為 -1 表示不限定幀率。
轉自http://blog.csdn.net/huutu
一般在手機游戲中我們限定幀率為30 就OK了。
Application.targetFrameRate=30;
可是把這個代碼加入到project之后。在Unity中執行起來發現並沒有什么卵用。。。
。
轉自http://blog.csdn.net/huutu
於是到官網查看資料
http://docs.unity3d.com/ScriptReference/Application-targetFrameRate.html
Application.targetFrameRate public static int targetFrameRate; Description Instructs game to try to render at a specified frame rate. Setting targetFrameRate to -1 (the default) makes standalone games render as fast as they can, and web player games to render at 50-60 frames/second depending on the platform. Note that setting targetFrameRate does not guarantee that frame rate. There can be fluctuations due to platform specifics, or the game might not achieve the frame rate because the computer is too slow. If vsync is set in quality setting, the target framerate is ignored, and the vblank interval is used instead. The vBlankCount property on qualitysettings can be used to limit the framerate to half of the screens refresh rate (60 fps screen can be limited to 30 fps by setting vBlankCount to 2) targetFrameRate is ignored in the editor.
大意就是說:
Application.targetFrameRate 是用來讓游戲以指定的幀率執行。假設設置為 -1 就讓游戲以最快的速度執行。
可是 這個 設定會 垂直同步 影響。
假設設置了垂直同步,那么 就會拋棄這個設定 而依據 屏幕硬件的刷新速度來執行。
假設設置了垂直同步為1,那么就是 60 幀。
假設設置了為2 ,那么就是 30 幀。
點擊 菜單 Editor -> ProjectSetting -> QualitySettings 來打開渲染質量設置面板。
轉自http://blog.csdn.net/huutu
1、首先關掉垂直同步。如上圖。
設置幀率為100
然后執行后的幀率是 100.
2、設置垂直同步為1
能夠看到幀率為 60 幀左右跳動,全然無視了代碼中的設定。
轉自http://blog.csdn.net/huutu
3、設定垂直同步為 2
能夠看到幀率在 30幀左右跳動。
轉自http://blog.csdn.net/huutu
在游戲中顯示幀率代碼:
using UnityEngine; using System.Collections; using DG.Tweening; public class NewBehaviourScript : MonoBehaviour { private float m_LastUpdateShowTime=0f; //上一次更新幀率的時間; private float m_UpdateShowDeltaTime=0.01f;//更新幀率的時間間隔; private int m_FrameUpdate=0;//幀數; private float m_FPS=0; void Awake() { Application.targetFrameRate=100; } // Use this for initialization void Start () { m_LastUpdateShowTime=Time.realtimeSinceStartup; } // Update is called once per frame void Update () { m_FrameUpdate++; if(Time.realtimeSinceStartup-m_LastUpdateShowTime>=m_UpdateShowDeltaTime) { m_FPS=m_FrameUpdate/(Time.realtimeSinceStartup-m_LastUpdateShowTime); m_FrameUpdate=0; m_LastUpdateShowTime=Time.realtimeSinceStartup; } } void OnGUI() { GUI.Label(new Rect(Screen.width/2,0,100,100),"FPS: "+m_FPS); } }
官網文檔中的最后一句……經測試在編輯器狀態下是有效的。。