[Unity3D]查看与设置游戏帧数FPS

来源:互联网 发布:我知你好未再19楼 编辑:程序博客网 时间:2024/04/28 13:56
FPS是衡量游戏性能的一个重要指标,Unity是跨平台的引擎工具,所以没有统一限定他的帧速率。

在PC平台,一般说来是越高越好,FPS越高,游戏越流畅。

在手机平台,普遍的流畅指标为60帧,能跑到60帧,就是非常流畅的体验了,再高的话一来差别很小,二来帧数太高,会耗费CPU和GPU,会导致发热和耗电量大。

1.UNITY3D设置帧数FPS的方法

3.5以后的版本,可以直接设置Application.targetFrameRate。直接修改脚本就可以了。

using UnityEngine;
using System.Collections;

public class SetFPS : MonoBehaviour {
    void Awake() {
        Application.targetFrameRate = 60;//此处限定60帧
    }
}

默认参数为-1,这个时候所有平台中游戏都会尽可能快地渲染,在WEB平台上最高是50~60帧。

需要注意的是在Edit/Project Setting/QualitySettings下,若vsync被设置了,则targetFrameRate设置的将无效。两者是冲突关系。具体看下面的脚本介绍,最新的地址:

http://docs.unity3d.com/Documentation/ScriptReference/Application-targetFrameRate.html


2.怎么查看当前帧数FPS?
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. [System.Reflection.Obfuscation(Exclude = true)]  
  5. public class DebugScreen : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.       
  10.     }  
  11.       
  12.     // Update is called once per frame  
  13.     void Update () {  
  14.         UpdateTick();  
  15.     }  
  16.   
  17.     void OnGUI()  
  18.     {  
  19.         DrawFps();  
  20.     }  
  21.   
  22.     private void DrawFps()  
  23.     {  
  24.         if (mLastFps > 50)  
  25.         {  
  26.             GUI.color = new Color(0, 1, 0);  
  27.         }  
  28.         else if (mLastFps > 40)  
  29.         {  
  30.             GUI.color = new Color(1, 1, 0);  
  31.         }  
  32.         else  
  33.         {  
  34.             GUI.color = new Color(1.0f, 0, 0);  
  35.         }  
  36.   
  37.         GUI.Label(new Rect(50, 32, 64, 24), "fps: " + mLastFps);  
  38.               
  39.     }  
  40.   
  41.     private long mFrameCount = 0;  
  42.     private long mLastFrameTime = 0;  
  43.     static long mLastFps = 0;  
  44.     private void UpdateTick()  
  45.     {  
  46.         if (true)  
  47.         {  
  48.             mFrameCount++;  
  49.             long nCurTime = TickToMilliSec(System.DateTime.Now.Ticks);  
  50.             if (mLastFrameTime == 0)  
  51.             {  
  52.                 mLastFrameTime = TickToMilliSec(System.DateTime.Now.Ticks);  
  53.             }  
  54.   
  55.             if ((nCurTime - mLastFrameTime) >= 1000)  
  56.             {  
  57.                 long fps = (long)(mFrameCount * 1.0f / ((nCurTime - mLastFrameTime) / 1000.0f));  
  58.   
  59.                 mLastFps = fps;  
  60.   
  61.                 mFrameCount = 0;  
  62.   
  63.                 mLastFrameTime = nCurTime;  
  64.             }  
  65.         }  
  66.     }  
  67.     public static long TickToMilliSec(long tick)  
  68.     {  
  69.         return tick / (10 * 1000);  
  70.     }  
  71. }  


0 0
原创粉丝点击