修改Unity的FPS
FPS是游戲運行的幀數,本文講解如何修改Unity引擎的FPS。
步驟
1、在 Edit/Project Settings/Quality 質量設置里把幀數設定關閉,關閉之后才能在代碼中修改游戲運行的幀數。
UpdateFrame.cs
2、在Unity中創建新腳本UpdateFrame.cs ,代碼
using UnityEngine; using System.Collections; /// <summary> /// 功能:修改游戲FPS /// </summary> public class UpdateFrame : MonoBehaviour { //游戲的FPS,可在屬性窗口中修改 public int targetFrameRate = 300; //當程序喚醒時 void Awake () { //修改當前的FPS Application.targetFrameRate = targetFrameRate; } }
3、把該代碼及ShowFPS.js綁定在層次視圖的任一GameObject上
嘗試修改
4、運行游戲,即可以Game視圖中看到當前的FPS修改targetFrameRate變量,查看FPS的變化
ShowFPS.js
@script ExecuteInEditMode private var gui : GUIText; private var updateInterval = 1.0; private var lastInterval : double; // Last interval end time private var frames = 0; // Frames over current interval function Start() { lastInterval = Time.realtimeSinceStartup; frames = 0; } function OnDisable () { if (gui) DestroyImmediate (gui.gameObject); } function Update() { #if !UNITY_FLASH ++frames; var timeNow = Time.realtimeSinceStartup; if (timeNow > lastInterval + updateInterval) { if (!gui) { var go : GameObject = new GameObject("FPS Display", GUIText); go.hideFlags = HideFlags.HideAndDontSave; go.transform.position = Vector3(0,0,0); gui = go.guiText; gui.pixelOffset = Vector2(5,55); } var fps : float = frames / (timeNow - lastInterval); var ms : float = 1000.0f / Mathf.Max (fps, 0.00001); gui.text = ms.ToString("f1") + "ms " + fps.ToString("f2") + "FPS"; frames = 0; lastInterval = timeNow; } #endif }