首页 技术 正文
技术 2022年11月12日
0 收藏 315 点赞 4,136 浏览 2567 个字

在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;
DescriptionInstructs 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 来打开渲染质量设置面板。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHV1dHU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center” alt=””>

转自http://blog.csdn.net/huutu

1、首先关掉垂直同步。如上图。

设置帧率为100

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHV1dHU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center” alt=””>

然后执行后的帧率是 100.

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHV1dHU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center” alt=””>

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);
}
}

官网文档中的最后一句……经測试在编辑器状态下是有效的。。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901