您的位置:首页 > 移动开发 > Unity3D

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

2014-11-25 10:58 435 查看
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?

using UnityEngine;
using System.Collections;

[System.Reflection.Obfuscation(Exclude = true)]
public class DebugScreen : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
UpdateTick();
}

void OnGUI()
{
DrawFps();
}

private void DrawFps()
{
if (mLastFps > 50)
{
GUI.color = new Color(0, 1, 0);
}
else if (mLastFps > 40)
{
GUI.color = new Color(1, 1, 0);
}
else
{
GUI.color = new Color(1.0f, 0, 0);
}

GUI.Label(new Rect(50, 32, 64, 24), "fps: " + mLastFps);

}

private long mFrameCount = 0;
private long mLastFrameTime = 0;
static long mLastFps = 0;
private void UpdateTick()
{
if (true)
{
mFrameCount++;
long nCurTime = TickToMilliSec(System.DateTime.Now.Ticks);
if (mLastFrameTime == 0)
{
mLastFrameTime = TickToMilliSec(System.DateTime.Now.Ticks);
}

if ((nCurTime - mLastFrameTime) >= 1000)
{
long fps = (long)(mFrameCount * 1.0f / ((nCurTime - mLastFrameTime) / 1000.0f));

mLastFps = fps;

mFrameCount = 0;

mLastFrameTime = nCurTime;
}
}
}
public static long TickToMilliSec(long tick)
{
return tick / (10 * 1000);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: