栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

unity中显示fps

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unity中显示fps

使用OnGUI 显示窗口文字 1)fps

FPS的值的含义就是1秒钟update被执行了多少次。其计算原理就是先记一个初始时间,接着曲当前时间减去初始时间,这期间update()被执行的次数就是FPS了。

fps计算
using UnityEngine;
using UnityEngine.UI;
 
public class FpsUI : UIBase
{
    private Text FpsText;
    private float time;
    private int frameCount;
    private void Awake()
    {
        InitUI();
    }
    public override void InitUI()
    {
        FpsText = GetComp("FpsText");
    }
    void Update()
    {
        time += Time.unscaledDeltaTime;
        frameCount++;
        if (time >= 1 && frameCount >= 1)
        {
            float fps = frameCount / time;
            time = 0;
            frameCount = 0;
            FpsText.text = fps.ToString("f2");//#0.00
            FpsText.color = fps >= 20 ? Color.white : (fps > 15 ? Color.yellow : Color.red);
        }
    }
2)OnGUI

GUI是unity4.6版本之前的UI系统,因为其功能比较单一并且效率不高,已经被新版的UGUI所代替,如果想显示一些辅助信息或者调试按钮等,大多还会使用它。

OnGUI显示
void OnGUI()
    {
        GUIStyle guistyle = GUIStyle.none;//新建窗口样式
        guistyle.fontSize = 30; //字号大小
        guistyle.alignment = TextAnchor.UpperLeft;//对齐方式
        GUI.Label(new Rect(80, 60, 100, 100), posture, guistyle);//窗口方式,内容和样式

    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/900310.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号