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

unity中FPS的设置与显示

2015-09-08 15:01 429 查看
做项目时,想实时显示FPS,限定FPS来减轻CPU和GPU的负荷。

注意,要限定FPS数值需要在Edit->Project Setting->Qualit中的VSync count改为Don't Sync

代码根据unity圣典中Time.realtimeSinceStartup 自游戏开始实时时间下的例子修改:

using UnityEngine;
using System.Collections;

public class FpsScript : MonoBehaviour {

public float UpdateInterval = 0.5F;		//在每个updateInterval间隔处计算,帧/秒,这样显示就不会随意的改变

private float LastInterval;		// Last interval end time 最后间隔结束时间

private int Frames = 0;			// Frames over current interval 超过当前间隔帧

private float Fps;				// Current FPS //当前FPS

public UILabel label;		//在NGUI的Label显示

void Start()
{
//Application.targetFrameRate=30;		//限定帧数

LastInterval = Time.realtimeSinceStartup;  //自游戏开始实时时间

Frames = 0;
}

void OnGUI()
{
label.text = "FPS" + Fps.ToString ("f2");		//显示两位小数
}

void Update()
{
++Frames;

if (Time.realtimeSinceStartup > LastInterval + UpdateInterval)
{
Fps = Frames / (Time.realtimeSinceStartup - LastInterval);

Frames = 0;

LastInterval = Time.realtimeSinceStartup;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: