Skip to content

Instantly share code, notes, and snippets.

@SachinGanesh
Last active May 31, 2018 06:55
Show Gist options
  • Save SachinGanesh/e4c85da2fee6acdba632f740fe7000ea to your computer and use it in GitHub Desktop.
Save SachinGanesh/e4c85da2fee6acdba632f740fe7000ea to your computer and use it in GitHub Desktop.
FPSCounter for Unity3D C#
// Copyright 2014 Invex Games http://invexgames.com
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class FPSCounter : Singleton<FPSCounter>
{
public float updateInterval = 0.5f;
float deltaFps = 0f; // FPS accumulated over the interval
int frames = 0; // Frames drawn over the interval
float timeleft; // Left time for current interval
public Text theText;
public float FPS;
void Start()
{
timeleft = updateInterval;
}
void Update()
{
timeleft -= Time.deltaTime;
deltaFps += Time.timeScale/Time.deltaTime;
++frames;
// Interval ended - update GUI text and start new interval
if( timeleft <= 0f )
{
// display two fractional digits (f2 format)
FPS = deltaFps/frames;
theText.text = "" + (deltaFps/frames).ToString("f2") + " FPS";
if ((deltaFps/frames) < 1)
{
theText.text = "";
}
timeleft = updateInterval;
deltaFps = 0f;
frames = 0;
}
}
}
@SachinGanesh
Copy link
Author

Uses Singleton

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment