|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | + |
| 5 | +public class FrameCount : MonoBehaviour |
| 6 | +{ |
| 7 | + |
| 8 | + public static FrameCount Instance; |
| 9 | + |
| 10 | + [Header("=== Calculated (Read-Only) ===")] |
| 11 | + [Tooltip("The raw frame number of the current frame. Wil always increment from 0 at the start of the scene.")] |
| 12 | + [SerializeField, ReadOnlyInsp] private int _frame_count; |
| 13 | + [Tooltip("The raw frames-per-second calculated.")] |
| 14 | + [SerializeField, ReadOnlyInsp] private float _fps; |
| 15 | + [Tooltip("A smoothened FPS based on a ratio between the previous FPS calculated and the current raw FPS")] |
| 16 | + [SerializeField, ReadOnlyInsp] private float _smoothed_fps; |
| 17 | + private float _prev_fps = 0f; |
| 18 | + |
| 19 | + [Header("=== Settings ===")] |
| 20 | + [Range(0f,1f), Tooltip("The ratio for calculating the smoothed FPS. 1 = focus only on raw FPS on the current frame, 0 = focus only on the previous FPS of the previous frame.")] |
| 21 | + public float smoothed_ratio = 0.75f; |
| 22 | + |
| 23 | + // Outputs readable to other external scripts |
| 24 | + public int frame_count => _frame_count; |
| 25 | + public float fps => _fps; |
| 26 | + public float smoothed_fps => _smoothed_fps; |
| 27 | + |
| 28 | + private void Awake() { |
| 29 | + Instance = this; |
| 30 | + } |
| 31 | + |
| 32 | + private void Update() { |
| 33 | + _frame_count = Time.frameCount; |
| 34 | + _fps = 1f / Time.unscaledDeltaTime; |
| 35 | + _smoothed_fps = _prev_fps * (1f-smoothed_ratio) + _fps * smoothed_ratio; |
| 36 | + _prev_fps = _smoothed_fps; |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +} |
0 commit comments