using UnityEngine; namespace FirstGearGames.Utilities.Maths { public class FrameRateCalculator { #region Private. /// /// Time used to generate Frames. /// private float _timePassed = 0f; /// /// Frames performed in TimePassed. Float is used to reduce casting. /// private float _frames = 0; #endregion #region Const. /// /// How many frames to pass before slicing calculation values. /// private const int RESET_FRAME_COUNT = 60; /// /// Percentage to slice calculation values by. Higher percentages result in smoother frame rate adjustments. /// private const float CALCULATION_SLICE_PERCENT = 0.7f; #endregion /// /// Gets the current frame rate. /// /// public int GetIntFrameRate() { return Mathf.CeilToInt((_frames / _timePassed)); } /// /// Gets the current frame rate. /// /// public float GetFloatFrameRate() { return (_frames / _timePassed); } /// /// Updates frame count and time passed. /// public bool Update(float unscaledDeltaTime) { _timePassed += unscaledDeltaTime; _frames++; if (_frames > RESET_FRAME_COUNT) { _frames *= CALCULATION_SLICE_PERCENT; _timePassed *= CALCULATION_SLICE_PERCENT; return true; } else { return false; } } } }