99// --------------------------------------------------------------------------------------------------------------------
1010
1111using System ;
12+ using System . Diagnostics ;
1213using UnityEngine ;
1314
1415namespace 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,18 @@ private void Awake()
4051 {
4152 WebGlPlugins . HideInfoPanel ( ) ;
4253 }
43-
54+
55+ if ( trackFps )
56+ {
57+ WebGlPlugins . AddFpsTrackingEvent ( 0 ) ;
58+ }
59+
4460 if ( trackAwakeTime )
4561 {
4662 WebGlPlugins . AddTimeTrackingEvent ( "Awake" ) ;
4763 }
64+
65+ stopWatch = Stopwatch . StartNew ( ) ;
4866 }
4967
5068 private void Start ( )
@@ -54,5 +72,32 @@ private void Start()
5472 WebGlPlugins . AddTimeTrackingEvent ( "Start" ) ;
5573 }
5674 }
75+
76+ private void Update ( )
77+ {
78+ if ( ! trackFps )
79+ {
80+ this . enabled = false ;
81+ return ;
82+ }
83+
84+ if ( stopWatch . Elapsed . TotalSeconds > updateInterval )
85+ {
86+ var currentFrameCount = Time . frameCount ;
87+ var frameCount = currentFrameCount - lastFrameCount ;
88+ float fps = ( float ) ( frameCount / stopWatch . Elapsed . TotalSeconds ) ;
89+ WebGlPlugins . AddFpsTrackingEvent ( fps ) ;
90+ stopWatch . Restart ( ) ;
91+ lastFrameCount = currentFrameCount ;
92+ }
93+ }
94+
95+ private void OnApplicationPause ( bool pauseStatus ) {
96+ if ( pauseStatus ) {
97+ stopWatch . Stop ( ) ;
98+ } else {
99+ stopWatch . Start ( ) ;
100+ }
101+ }
57102 }
58103}
0 commit comments