ProTimer.cs 1008 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using UnityEngine;
  6. public class ProTimer :IDisposable
  7. {
  8. private string name;
  9. private int times;
  10. private Stopwatch watch;
  11. private float _runTime;
  12. public ProTimer(string name) : this(name, 0)
  13. {
  14. }
  15. public ProTimer(string name, int times)
  16. {
  17. this.name = name;
  18. this.times = times;
  19. if (this.times <= 0)
  20. {
  21. this.times = 1;
  22. }
  23. watch = Stopwatch.StartNew();
  24. }
  25. public float RunTime { get => _runTime; }
  26. public void Dispose()
  27. {
  28. watch.Stop();
  29. float ms = watch.ElapsedMilliseconds;
  30. _runTime = ms;
  31. if (times > 1)
  32. {
  33. UnityEngine.Debug.Log($"ProTimer:{name} finished {times} total{ms} ms, per time {ms / times} ms");
  34. }
  35. else
  36. {
  37. UnityEngine.Debug.Log($"ProTimer:{name} finished total{ms} ms");
  38. }
  39. }
  40. }