OnlineTimer.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Mirror;
  5. using System.Diagnostics;
  6. using Debug = UnityEngine.Debug;
  7. public class OnlineTimer : NetworkBehaviour
  8. {
  9. private Stopwatch stopwatch;
  10. // Start is called before the first frame update
  11. private void Awake()
  12. {
  13. stopwatch = new Stopwatch();
  14. }
  15. public override void OnStartClient()
  16. {
  17. stopwatch.Reset();
  18. stopwatch.Start();
  19. Debug.Log("Stopwatch started!");
  20. base.OnStartClient();
  21. }
  22. public void OnDisable()
  23. {
  24. if(stopwatch.IsRunning)
  25. {
  26. System.TimeSpan ts = stopwatch.Elapsed;
  27. stopwatch.Stop();
  28. Debug.Log("Stopwatch stopped: duration " + string.Format("{0:00}:{1:00}:{2:00}.{3:00}",
  29. ts.Hours, ts.Minutes, ts.Seconds,
  30. ts.Milliseconds / 10));
  31. }
  32. }
  33. private void OnGUI()
  34. {
  35. if (!stopwatch.IsRunning) return;
  36. GUI.Box(new Rect(new Vector2(2, Screen.height - 36), new Vector2(320, 32)), "ONLINE TIME: " + string.Format("{0:00}:{1:00}:{2:00}.{3:00}",
  37. stopwatch.Elapsed.Hours, stopwatch.Elapsed.Minutes, stopwatch.Elapsed.Seconds,
  38. stopwatch.Elapsed.Milliseconds / 10));
  39. }
  40. }