using System; using System.Linq; using UnityEngine; using UnityEngine.UI; namespace ZC { public class FPSCounter : MonoBehaviour { [SerializeField] private Text _txtTime; [SerializeField] private Text _txtFps; [SerializeField] private int _fpsCount = 10; private float[] _fpsArray ; private float _avr; private float _min; private float _max; private DateTime _startTime; private void Start() { _fpsArray = new float[_fpsCount]; _startTime = DateTime.Now; } private void Update() { _fpsArray[Time.frameCount % _fpsArray.Length] = 1f / Time.deltaTime; if (Time.frameCount % this._fpsArray.Length == 0) { _avr = this._fpsArray.Sum() / this._fpsArray.Length; _min = float.MaxValue; _max = float.MinValue; foreach (var f in this._fpsArray) { if (f < _min) _min = f; if (f > _max) _max = f; } this._txtFps.text = $"Avr:{this._avr:n0}\nMin:{this._min:n0}\nMax:{this._max:n0}"; } var offsetTime = DateTime.Now - this._startTime; this._txtTime.text = $"Time:{offsetTime.Hours}:{offsetTime.Minutes}:{offsetTime.Seconds}.{offsetTime.Milliseconds}"; } } }