Unity3D】如何获取游戏时的帧数?

2021-03-04 12:01发布

9条回答

游戏中的帧率FPS:单位秒内执行了多少帧,在unity中的实现如下:


代码:


using System.Collections;

using System.Collections.Generic;

using UnityEngine;


///

/// FPS显示

///

public class lmf006 : MonoBehaviour

{


    ///

    /// 更新间隔

    ///

    private float updateInterval = 0.5f;


    ///

    /// 累加值

    ///

    private float accum = 0;


    ///

    /// 帧频

    ///

    private int frames = 0;


    ///

    /// 计时数值

    ///

    private float timeLeft;


    ///

    /// FPS显示字符

    ///

    private string strFPSValue;



    // Start is called before the first frame update

    void Start()

    {

        timeLeft = updateInterval;

    }


    // Update is called once per frame

    void Update()

    {

        timeLeft -= Time.deltaTime;

        accum += Time.timeScale / Time.deltaTime;  //每秒执行了多少帧

        ++frames;                                  // 帧频

        if (timeLeft <= 0.0)

        {

            float fps = accum / frames;             //多次后取平均值

            string formate = System.String.Format("{0:F2} FPS",fps);

            strFPSValue = formate;

            timeLeft = updateInterval;

            accum = 0.0f;

            frames = 0;

        }

    }


    void OnGUI()

    {

        GUIStyle gUIStyle = GUIStyle.none;

        gUIStyle.fontSize = 60;

        gUIStyle.normal.textColor = Color.red;

        gUIStyle.alignment = TextAnchor.UpperLeft;

        Rect rect = new Rect(40, 0, 100, 100);

        GUI.Label(rect, strFPSValue, gUIStyle);

    }


}



老杜
3楼 · 2021-03-05 14:54

在游戏过程中一般人不觉得卡顿的FPS频率大约是30Hz,想要达到流畅等级则需要60Hz,

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


///

/// FPS 显示于OnGUI 

///

public class FPSOnGUIText : MonoBehaviour {


 float updateInterval = 1.0f;   //当前时间间隔

 private float accumulated = 0.0f;  //在此期间累积 

 private float frames = 0;    //在间隔内绘制的帧 

 private float timeRemaining;   //当前间隔的剩余时间

 private float fps = 15.0f;    //当前帧 Current FPS

 private float lastSample;


 void Start()

 {

  DontDestroyOnLoad(this.gameObject); //不销毁此游戏对象,在哪个场景都可以显示,,不需要则注释

  timeRemaining = updateInterval;

  lastSample = Time.realtimeSinceStartup; //实时自启动

 }

 

 void Update()

 {

  ++frames;

  float newSample = Time.realtimeSinceStartup;

  float deltaTime = newSample - lastSample;

  lastSample = newSample;

  timeRemaining -= deltaTime;

  accumulated += 1.0f / deltaTime;

 

  if (timeRemaining <= 0.0f)

  {

   fps = accumulated / frames;

   timeRemaining = updateInterval;

   accumulated = 0.0f;

   frames = 0;

  }

 }


 void OnGUI()

 {

  GUIStyle style = new GUIStyle

  {

   border = new RectOffset(10, 10, 10, 10),

   fontSize = 50,

   fontStyle = FontStyle.BoldAndItalic,   

  };

  //自定义宽度 ,高度大小 颜色,style

  GUI.Label(new Rect(Screen.width/2-50, Screen.height - 100, 200, 200), "" + "FPS:" + fps.ToString("f2")+ "", style);

 }

}


花轮同学
4楼 · 2021-03-05 17:16

通过编写脚本的方式来获取

visonx
5楼 · 2021-03-08 10:45

通过编写脚本的方式来获取。

小狮子
6楼 · 2021-03-15 11:22

在game场景中既可以查看 image.png

梅向南
7楼 · 2021-03-16 09:40


using System.Collections;

using UnityEngine;

 

public class FrameRateManager : MonoBehaviour

{

    [Header("OnGUI for frame rate---")]

    public Color textColor = Color.white;

    public int guiFontSize = 50;

    private string label = string.Empty;

    private GUIStyle style = new GUIStyle();

    private float count;

 

    private void Awake()

    {

        // set target frame rate

        Application.targetFrameRate = 60;

    }

 

    private IEnumerator Start()

    {

        while (true)

        {

            count = 1f / Time.deltaTime;

            label = string.Format("{0:N2}", count);

            yield return new WaitForSeconds(0.2f);

 

        }

    }

    // show fps

    private void OnGUI()

    {

        style.fontSize = guiFontSize;

        style.normal.textColor = textColor; 

        GUI.Label(new Rect(300f, 200f, 500f, 300f), this.label, this.style);

    }

}


蜗牛
8楼 · 2021-03-27 12:30

游戏中的帧率FPS:单位秒内执行了多少帧,在unity中的实现如下:


代码:


using System.Collections;

using System.Collections.Generic;

using UnityEngine;


///

/// FPS显示

///

public class lmf006 : MonoBehaviour

{


    ///

    /// 更新间隔

    ///

    private float updateInterval = 0.5f;


    ///

    /// 累加值

    ///

    private float accum = 0;


    ///

    /// 帧频

    ///

    private int frames = 0;


    ///

    /// 计时数值

    ///

    private float timeLeft;


    ///

    /// FPS显示字符

    ///

    private string strFPSValue;



    // Start is called before the first frame update

    void Start()

    {

        timeLeft = updateInterval;

    }


    // Update is called once per frame

    void Update()

    {

        timeLeft -= Time.deltaTime;

        accum += Time.timeScale / Time.deltaTime;  //每秒执行了多少帧

        ++frames;                                  // 帧频

        if (timeLeft <= 0.0)

        {

            float fps = accum / frames;             //多次后取平均值

            string formate = System.String.Format("{0:F2} FPS",fps);

            strFPSValue = formate;

            timeLeft = updateInterval;

            accum = 0.0f;

            frames = 0;

        }

    }


    void OnGUI()

    {

        GUIStyle gUIStyle = GUIStyle.none;

        gUIStyle.fontSize = 60;

        gUIStyle.normal.textColor = Color.red;

        gUIStyle.alignment = TextAnchor.UpperLeft;

        Rect rect = new Rect(40, 0, 100, 100);

        GUI.Label(rect, strFPSValue, gUIStyle);

    }


}



Mantra
9楼 · 2021-04-15 11:13

可以通过Time.deltaTime计算获得,因为这个值代表当前帧消耗的时间。

具体脚本可以参考楼上的代码。

相关问题推荐

  • 回答 17

    还是要学好编程基础呀如果你觉得编程很苦难 不一定要从c#开始学  学学js flash as等等  有个梯度就好多了如果要用好unity  不会编程那是不行的  学习的过程中都有个头疼的过程  记住  头越痛  代表你要接受的东西越多  坚持 你的大脑在和知识兼容中:D...

  • unity如何自学Unity3D 2022-01-06 15:24
    回答 18

    可以先思考学习的目的,是什么因素在驱动你。是完成一款作品?进入某个行业?还是探究某类问题?否则和技术相关的知识浩如烟海,很容易迷失在细枝末节上。而要找到动力源头。个人的经验,就是关注一些和自己同方向,同类型的创作者。他们输出的作品会激励你,...

  • 回答 23

    可以让模型师直接作出这样的形状,如果用纯Unity制作,就要用基本游戏对象拼接了,包括楼梯,城堡,都可以拼接出来。正常情况不会这样做,因为不够精美,都是建模师来实现,毕竟Unity不属于专业的建模软件,侧重于实现功能。...

  • 回答 2

    Shader Unlit/Test{Properties{_MainTex(MainTex,2D)=white{}_MainColor(MainColor,COLOR)=(1,1,1,1)_AddTex(AddTex,2D)=white{}_Maxset(Max,Range(0.1,1.0))=0.1}SubShader{Tags{RenderType=Transparent Queue=Tran...

  • 回答 4

    文章主要为大家详细介绍了Unity Shader实现水波纹效果,文中示例代码介绍的非常详细具体代码实现如下:Shader Custom/shuibowen{ Properties{ _MainTex(Base (RGB),2D)=white{} _distanceFactor(Distancefactor,float)=1 _timeFactor(time factor,float)=...

  • 回答 7

    策划的最基本的原则就是:改进缺点,做别人没有做到的。无论游戏策划还是其它策划都是一样!    游戏策划的第二个原则:放飞思想。也许你认为我是说策划们应该充满想象力,能想一些匪夷所思的东西!对不起。不是这意思!一个合格的策划不是为了发泄自己的...

  • 回答 7
    已采纳

    可以多玩一些其它的游戏,看一些科幻电影等,寻找灵感。

  • 回答 3
    已采纳

    游戏架构与设计不纯粹是一门科学,它不需要提出假设或探究真理,也不被逻辑或正规方法的严格标准所束缚。游戏的目的就是通过玩来获得娱乐,因此游戏设计即需要艺术家一样的创造力,也需要工程师一样的精心规划。游戏设计是一门手艺,就像好莱坞的电影摄像或服...

  • 回答 5

    void Update(){          transform.rotation = Quaternion.Euler(Vector3.zero);}可以试一下,保证物体x轴和z轴为0就可以使其一直垂直。

  • 回答 3

    界面左右移动、上下移动。。本质都是:手指滑动。。。可以参考这些:https://www.cnblogs.com/coldcode/p/5362537.htmlhttps://blog.csdn.net/totosj/article/details/80112852https://blog.csdn.net/zcc858079762/article/details/85253120...

  • 回答 6

    首先新建一个C#脚本,命名为MyFollow,然后把以下代码粘贴进去,保存:AخA 1using UnityEngine;2using System.Collections;3public class MyFollow : MonoBehaviour4{5    public float distanceAway = 5;          // distance...

  • 回答 5

    安装高通的Vuforia插件即可。

  • 回答 5

    不可以,只能一个工程打一个包。

  • 回答 2

    Edit->Project Settings->Graphics 找到Shader Stripping 中fog mode设置为custom(原来是Automatic),然后选中你想要的模式,最后重新打包就ok

  • 回答 2

    用到的插件:System.Drawing.dllSystem.Windows.Forms.dllSystem.Deployment.dll(运用基于.Net4.x的dll打包时,需要用到该dll,否则会报错)代码如下:using System;using System.Runtime.InteropServices;using UnityEngine;using UnityEngine.UI; p......

  • 回答 6

    如下图,设置为none,然后删掉滑动条就可以了。

没有解决我的问题,去提问