using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class FPS : MonoBehaviour
{
	public float updateInterval = 1f;
	public float fps { get; protected set; }
	public Text textFPS = null;

	protected float accum = 0;
	protected int frames = 0;
	protected float timeleft;

	protected void Start()
	{
		timeleft = updateInterval;
	}

	protected void Update()
	{
		timeleft -= Time.deltaTime;
		accum += Time.timeScale / Time.deltaTime;
		++frames;

		if (timeleft <= 0.0f)
		{
			fps = accum / frames;
			timeleft = updateInterval;
			accum = 0.0f;
			frames = 0;
		}

		if (textFPS != null)
		{
			textFPS.text = "FPS:"+Mathf.FloorToInt(fps).ToString();
		}
	}
}
