Skip to content

Commit a5ac6d5

Browse files
committed
Added showfps command.
1 parent b78be39 commit a5ac6d5

File tree

1 file changed

+120
-2
lines changed

1 file changed

+120
-2
lines changed

Runtime/DevConsoleMono.cs

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ internal sealed class DevConsoleMono : MonoBehaviour
5050
private const float MaxConsoleHeight = 900;
5151
private const int CommandHistoryLength = 10;
5252
private const int MaxCachedEnumTypes = 6;
53+
private const float FpsUpdateRate = 4f;
5354

5455
#region Input constants
5556

@@ -88,6 +89,7 @@ internal sealed class DevConsoleMono : MonoBehaviour
8889
private const string PrefDisplayUnityErrors = "DevConsole.displayUnityErrors";
8990
private const string PrefDisplayUnityExceptions = "DevConsole.displayUnityExceptions";
9091
private const string PrefDisplayUnityWarnings = "DevConsole.displayUnityWarnings";
92+
private const string PrefShowFps = "DevConsole.displayFps";
9193

9294
#endregion
9395

@@ -173,6 +175,19 @@ internal sealed class DevConsoleMono : MonoBehaviour
173175

174176
#endregion
175177

178+
#region Fps fields
179+
180+
private bool _isDisplayingFps;
181+
private float _fpsDeltaTime;
182+
private int _fps;
183+
private float _fpsMs;
184+
private float _fpsElapsed;
185+
private GUIStyle _fpsStyle;
186+
private Vector2 _fpsLabelSize;
187+
private Color _fpsTextColour;
188+
189+
#endregion
190+
176191
#endregion
177192

178193
#region Properties
@@ -624,6 +639,7 @@ private void Awake()
624639

625640
LoadPreferences();
626641
InitBuiltInCommands();
642+
InitBuiltInParsers();
627643
InitAttributeCommands();
628644

629645
// Enable the console by default if in editor or a development build
@@ -693,6 +709,35 @@ private void LateUpdate()
693709
return;
694710
}
695711

712+
// Update fps display
713+
if (_isDisplayingFps)
714+
{
715+
_fpsDeltaTime += (Time.unscaledDeltaTime - _fpsDeltaTime) * 0.1f;
716+
_fpsElapsed += Time.deltaTime;
717+
if (_fpsElapsed > 1.0f / FpsUpdateRate)
718+
{
719+
// Calculate fps values
720+
_fpsMs = _fpsDeltaTime * 1000f;
721+
_fps = Mathf.RoundToInt(1.0f / _fpsDeltaTime);
722+
_fpsElapsed -= 1.0f / FpsUpdateRate;
723+
724+
// Determine colour
725+
_fpsTextColour = Color.white;
726+
if (Application.targetFrameRate == -1 && _fps >= 60 || Application.targetFrameRate != -1 && _fps >= Application.targetFrameRate)
727+
{
728+
_fpsTextColour = Color.green;
729+
}
730+
else if (_fps < 10)
731+
{
732+
_fpsTextColour = Color.red;
733+
}
734+
else if (_fps < 30 && (Application.targetFrameRate > 30 || Application.targetFrameRate == -1))
735+
{
736+
_fpsTextColour = Color.yellow;
737+
}
738+
}
739+
}
740+
696741
// Check bindings (as long as the input field isn't focused!)
697742
if (BindingsIsEnabled && !_inputField.isFocused)
698743
{
@@ -766,6 +811,46 @@ private void LateUpdate()
766811
}
767812
}
768813

814+
private void OnGUI()
815+
{
816+
if (!ConsoleIsEnabled)
817+
{
818+
return;
819+
}
820+
821+
if (_isDisplayingFps)
822+
{
823+
if (_fpsStyle == null)
824+
{
825+
// Create the style
826+
_fpsStyle = new GUIStyle(GUI.skin.box)
827+
{
828+
alignment = TextAnchor.MiddleCenter,
829+
fontSize = 20,
830+
normal = { textColor = Color.white, background = Texture2D.whiteTexture }
831+
};
832+
833+
_fpsLabelSize = _fpsStyle.CalcSize(new GUIContent("0.00 ms (000 fps)"));
834+
}
835+
836+
Color oldBackgroundColour = GUI.backgroundColor;
837+
Color oldContentColour = GUI.contentColor;
838+
839+
// Set colours
840+
GUI.backgroundColor = new Color(0f, 0f, 0f, 0.75f);
841+
GUI.contentColor = _fpsTextColour;
842+
843+
// Create label
844+
GUI.Box(
845+
new Rect(10, 10, _fpsLabelSize.x + 10f, _fpsLabelSize.y + 10f),
846+
$"{_fpsMs:0.00} ms ({_fps:0.} fps)",
847+
_fpsStyle);
848+
849+
GUI.backgroundColor = oldBackgroundColour;
850+
GUI.contentColor = oldContentColour;
851+
}
852+
}
853+
769854
private void OnDestroy()
770855
{
771856
SavePreferences();
@@ -1078,6 +1163,32 @@ private void InitBuiltInCommands()
10781163
() => LogVariable("Application path", AppDomain.CurrentDomain.BaseDirectory)
10791164
));
10801165

1166+
AddCommand(Command.Create<bool>(
1167+
"showfps",
1168+
"displayfps",
1169+
"Query or set whether the fps is being displayed on-screen",
1170+
Parameter.Create("enabled", "Whether the fps is being displayed on-screen"),
1171+
b =>
1172+
{
1173+
if (b != _isDisplayingFps)
1174+
{
1175+
_isDisplayingFps = !_isDisplayingFps;
1176+
1177+
if (_isDisplayingFps)
1178+
{
1179+
_fps = 0;
1180+
_fpsMs = 0f;
1181+
_fpsDeltaTime = 0f;
1182+
_fpsElapsed = 0f;
1183+
_fpsStyle = null;
1184+
}
1185+
}
1186+
1187+
LogSuccess($"{(b ? "Enabled" : "Disabled")} the on-screen fps.");
1188+
},
1189+
() => LogVariable("Show fps", _isDisplayingFps)
1190+
));
1191+
10811192
#endregion
10821193

10831194
#region Screen commands
@@ -1142,8 +1253,8 @@ private void InitBuiltInCommands()
11421253
));
11431254

11441255
AddCommand(Command.Create<int>(
1145-
"fps_target",
1146-
"fps_max",
1256+
"targetfps",
1257+
"",
11471258
"Query or set the target frame rate.",
11481259
Parameter.Create("targetFrameRate", "Frame rate the application will try to render at."),
11491260
i =>
@@ -1520,6 +1631,11 @@ void logChildren(GameObject obj, int tabAmount)
15201631
#endregion
15211632
}
15221633

1634+
private void InitBuiltInParsers()
1635+
{
1636+
1637+
}
1638+
15231639
private void InitAttributeCommands()
15241640
{
15251641
// https://github.com/yasirkula/UnityIngameDebugConsole/blob/master/Plugins/IngameDebugConsole/Scripts/DebugLogConsole.cs
@@ -2000,6 +2116,7 @@ private void SavePreferences()
20002116
DevConsoleData.SetObject(PrefDisplayUnityErrors, _displayUnityErrors);
20012117
DevConsoleData.SetObject(PrefDisplayUnityExceptions, _displayUnityExceptions);
20022118
DevConsoleData.SetObject(PrefDisplayUnityWarnings, _displayUnityWarnings);
2119+
DevConsoleData.SetObject(PrefShowFps, _isDisplayingFps);
20032120

20042121
DevConsoleData.Save();
20052122
}
@@ -2014,6 +2131,7 @@ private void LoadPreferences()
20142131
_displayUnityErrors = DevConsoleData.GetObject(PrefDisplayUnityErrors, true);
20152132
_displayUnityExceptions = DevConsoleData.GetObject(PrefDisplayUnityExceptions, true);
20162133
_displayUnityWarnings = DevConsoleData.GetObject(PrefDisplayUnityWarnings, true);
2134+
_isDisplayingFps = DevConsoleData.GetObject(PrefShowFps, false);
20172135

20182136
DevConsoleData.Clear();
20192137
}

0 commit comments

Comments
 (0)