Skip to content

Commit 2e7ea38

Browse files
committed
Add FPS tracking
1 parent caea164 commit 2e7ea38

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

Assets/Plugins/WebGL/WebGLTools/WebGlPlugins.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public static class WebGlPlugins
2525
[DllImport("__Internal")]
2626
private static extern void _AddTimeTrackingEvent(string eventName);
2727
[DllImport("__Internal")]
28+
private static extern void _AddFpsTrackingEvent(float fps);
29+
[DllImport("__Internal")]
2830
private static extern void _ShowInfoPanel();
2931
[DllImport("__Internal")]
3032
private static extern void _HideInfoPanel();
@@ -68,6 +70,15 @@ public static void AddTimeTrackingEvent(string eventName)
6870
#endif
6971
}
7072

73+
public static void AddFpsTrackingEvent(float fps)
74+
{
75+
#if UNITY_WEBGL && !UNITY_EDITOR
76+
_AddFpsTrackingEvent(fps);
77+
#else
78+
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(AddFpsTrackingEvent)} called with {fps:0.00}");
79+
#endif
80+
}
81+
7182
/// <summary>
7283
/// Show the info panel in the top right corner
7384
/// By default triggered by <see cref="WebGlTimeTracker"/> in Awake

Assets/Plugins/WebGL/WebGLTools/WebGlPlugins.jslib

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ var WebGlPlugins =
4949
console.log('Time tracker event ' +eventNameText +': ' + currentTimeRounded + 'ms');
5050
},
5151

52+
_AddFpsTrackingEvent: function(fps) {
53+
if(typeof onFpsTrackingEvent !== 'undefined') {
54+
onFpsTrackingEvent(fps);
55+
}
56+
},
57+
5258
_ShowInfoPanel: function () {
5359
if(typeof setInfoPanelVisible !== 'undefined') {
5460
setInfoPanelVisible(true);

Assets/Plugins/WebGL/WebGLTools/WebGlTimeTracker.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// --------------------------------------------------------------------------------------------------------------------
1010

1111
using System;
12+
using System.Diagnostics;
1213
using UnityEngine;
1314

1415
namespace Supyrb
@@ -21,15 +22,25 @@ namespace Supyrb
2122
[DefaultExecutionOrder(-100)]
2223
public class WebGlTimeTracker : MonoBehaviour
2324
{
24-
[SerializeField]
25+
[SerializeField]
2526
private bool showInfoPanelByDefault = true;
26-
27+
2728
[SerializeField]
2829
private bool trackAwakeTime = true;
29-
30+
3031
[SerializeField]
3132
private bool trackStartTime = true;
3233

34+
[Header("FPS Tracking")]
35+
[SerializeField]
36+
private bool trackFps = true;
37+
38+
[SerializeField]
39+
private float updateInterval = 0.5f;
40+
41+
private Stopwatch stopWatch;
42+
private int lastFrameCount;
43+
3344
private void Awake()
3445
{
3546
if (showInfoPanelByDefault)
@@ -40,11 +51,13 @@ private void Awake()
4051
{
4152
WebGlPlugins.HideInfoPanel();
4253
}
43-
54+
4455
if (trackAwakeTime)
4556
{
4657
WebGlPlugins.AddTimeTrackingEvent("Awake");
4758
}
59+
60+
stopWatch = Stopwatch.StartNew();
4861
}
4962

5063
private void Start()
@@ -54,5 +67,32 @@ private void Start()
5467
WebGlPlugins.AddTimeTrackingEvent("Start");
5568
}
5669
}
70+
71+
private void Update()
72+
{
73+
if(!trackFps)
74+
{
75+
this.enabled = false;
76+
return;
77+
}
78+
79+
if(stopWatch.Elapsed.TotalSeconds > updateInterval)
80+
{
81+
var currentFrameCount = Time.frameCount;
82+
var frameCount = currentFrameCount - lastFrameCount;
83+
float fps = (float)(frameCount / stopWatch.Elapsed.TotalSeconds);
84+
WebGlPlugins.AddFpsTrackingEvent(fps);
85+
stopWatch.Restart();
86+
lastFrameCount = currentFrameCount;
87+
}
88+
}
89+
90+
private void OnApplicationPause(bool pauseStatus) {
91+
if (pauseStatus) {
92+
stopWatch.Stop();
93+
} else {
94+
stopWatch.Start();
95+
}
96+
}
5797
}
5898
}

Assets/WebGLTemplates/Develop/debug-console.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,11 @@ function onAddTimeTracker(eventName) {
588588
refreshTrackingDiv();
589589
}
590590

591+
function onFpsTrackingEvent(fps) {
592+
const fpsDiv = getOrCreateInfoEntry('fps');
593+
fpsDiv.textContent = `FPS: ${fps.toFixed(1)}`;
594+
}
595+
591596
function refreshTrackingDiv() {
592597
const trackingDiv = getOrCreateInfoEntry('tracking');
593598
let innerHtml = '<dl>';

0 commit comments

Comments
 (0)