Unity3d开发_课本


第四章游戏脚本

使用OnGUI显示FPS

显示FPS

public float updateInterval = 0.5f;
public int test;
private float accum = 0;
private int frames = 0;
private float timeleft;
private string stringFps;
void Start()
{
    timeleft = updateInterval;
}
void Update()
{
    timeleft -= Time.deltaTime;
    accum += Time.timeScale / Time.deltaTime;
    ++frames;
    if (timeleft <= 0.0f)
    {
        float fps = accum / frames;
        string format = System.String.Format("{0:F2} FPS", fps);
        stringFps = format;
        timeleft = updateInterval;
        accum = 0.0f;
        frames = 0;
    }
}
void OnGUI()
{
    GUIStyle guistyle = GUIStyle.none;
    guistyle.fontSize = 30;
    guistyle.normal.textColor = Color.red;
    guistyle.alignment = TextAnchor.UpperLeft;
    Rect rt = new Rect(40, 0, 100, 100);
    GUI.Label(rt, stringFps,guistyle);
}

下边的代码强制设置fps最高帧为30帧要使用int 而不能使用float类型的参数

Application.targetFrameRate=30;

文章作者: 毛豆不逗比
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 毛豆不逗比 !
  目录
{% include '_third-party/exturl.swig' %} {% include '_third-party/bookmark.swig' %} {% include '_third-party/copy-code.swig' %} + {% include '_custom/custom.swig' %}