Common(十六)—— ShowFPS显示帧数

来源:互联网 发布:python tuple 和 array 编辑:程序博客网 时间:2024/05/02 01:03
目录为:Assets/Scripts/Common/ShowFPS.cs这个脚本挂到一个GameObject上就可以了。

这里写图片描述

using UnityEngine;using System.Collections;public class ShowFPS: MonoBehaviour{    public static ShowFPS Instance    {        private set;        get;    }    void OnEnable()    {        Instance = this;    }    void OnDisable()    {        Instance = null;    }    public float f_UpdateInterval = 0.5f;    private float f_LastInterval;    //总的渲染的帧数    private int i_Frames = 0;    private float f_Fps;    //用这个可以改变默认GUI风格    private GUIStyle style = new GUIStyle ();    public float sSPing    {        get;        set;    }    public float cSPing    {        get;        set;    }    void Start()    {        //游戏会尽量以这个帧率运行        Application.targetFrameRate = 300;        f_LastInterval = Time.realtimeSinceStartup;        i_Frames = 0;        //设置GUI的风格        style.fontSize = 10;        style.normal.textColor = new Color (0, 255, 0, 255);    }    void OnGUI()    {        GUI.Label (new Rect (0, 0, 200, 200), "FPS:" + f_Fps.ToString ("f2"), style);        GUI.Label (new Rect (0, 10, 200, 200), "sSPing:" + sSPing.ToString("f2"), style);        GUI.Label (new Rect (0, 20, 200, 200), "cSPing:" + cSPing.ToString ("f2"), style);    }    void Update()    {        ++i_Frames;        //f_UpdateInterval = 0.5f;        //这里表示每隔0.5s计算一次帧数        if (Time.realtimeSinceStartup > f_LastInterval + f_UpdateInterval)        {            //间隔时间内,渲染了多少帧,算出每秒帧数            f_Fps = i_Frames / (Time.realtimeSinceStartup - f_LastInterval);            i_Frames = 0;            f_LastInterval = Time.realtimeSinceStartup;        }    }}
阅读全文
0 0
原创粉丝点击