From 68f0cc16e117bae6c23379a4e9bddf451384e31f Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Thu, 22 Jul 2021 13:31:00 +0200 Subject: [PATCH] Added a framerate counter and let the game run at 120 fps on next-gen console hardware --- Assets/Scenes/ConsoleTest.unity | 2 +- Assets/Scripts/ConsoleTest.cs | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Assets/Scenes/ConsoleTest.unity b/Assets/Scenes/ConsoleTest.unity index 1b9734a..c8c1905 100644 --- a/Assets/Scenes/ConsoleTest.unity +++ b/Assets/Scenes/ConsoleTest.unity @@ -155,7 +155,7 @@ MonoBehaviour: m_EditorClassIdentifier: baseGame: 0 mod: - args: + args: +host_maxfps 144 mainCamera: {fileID: 963194227} visualStyles: - {fileID: 11400000, guid: d187fe54fb9a3e047bf4cec083877e72, type: 2} diff --git a/Assets/Scripts/ConsoleTest.cs b/Assets/Scripts/ConsoleTest.cs index 0e1d7be..0847742 100644 --- a/Assets/Scripts/ConsoleTest.cs +++ b/Assets/Scripts/ConsoleTest.cs @@ -23,6 +23,11 @@ public class ConsoleTest : MonoBehaviour { Debug.Log($"Running in {IntPtr.Size * 8}-bit mode"); +#if UNITY_PS5 || UNITY_GAMECORE_SCARLETT + Application.targetFrameRate = 120; + Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, true, 120); +#endif + uq = gameObject.AddComponent(); uq.BaseGame = baseGame; uq.ModDirectory = mod; @@ -40,4 +45,35 @@ public class ConsoleTest : MonoBehaviour return args.Split(new[] {' ', '\t', '\r', '\n'}, StringSplitOptions.RemoveEmptyEntries); } + + private readonly float[] frameTimes = new float[10]; + private int frameCount = 0; + private float framesPerSecond = float.NaN; + private string fpsText; + + private void Update() + { + frameTimes[frameCount++] = Time.unscaledDeltaTime; + if (frameCount >= frameTimes.Length) + { + float totalTime = 0f; + for (int i = 0; i < frameTimes.Length; ++i) + { + totalTime += frameTimes[i]; + } + + framesPerSecond = (float)frameCount / totalTime; + frameCount = 0; + + fpsText = $"{framesPerSecond:0.0} fps"; + } + } + + private void OnGUI() + { + GUI.matrix = Matrix4x4.Scale(new Vector3(3, 3, 1)); + + if (fpsText != null) + GUI.Label(new Rect(10, 10, 1000, 100), fpsText); + } }