Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditorTimer.cs 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #if SERVICES_SDK_CORE_ENABLED
  2. using System;
  3. using UnityEditor;
  4. namespace UnityEngine.Advertisements.Editor
  5. {
  6. class EditorTimer
  7. {
  8. public event Action Elapsed;
  9. double m_IntervalInSeconds;
  10. public double IntervalInSeconds
  11. {
  12. get => m_IntervalInSeconds;
  13. set
  14. {
  15. m_IntervalInSeconds = Math.Max(0, value);
  16. if (IsRunning())
  17. {
  18. Tick();
  19. }
  20. }
  21. }
  22. double m_StartTime;
  23. public bool IsRunning()
  24. {
  25. return m_StartTime > 0;
  26. }
  27. public void Restart()
  28. {
  29. m_StartTime = EditorApplication.timeSinceStartup;
  30. EditorApplication.update += Tick;
  31. }
  32. public void Stop()
  33. {
  34. EditorApplication.update -= Tick;
  35. m_StartTime = 0;
  36. }
  37. void Tick()
  38. {
  39. if (!IsElapsed())
  40. {
  41. return;
  42. }
  43. Stop();
  44. Elapsed?.Invoke();
  45. }
  46. bool IsElapsed()
  47. {
  48. return EditorApplication.timeSinceStartup - m_StartTime >= IntervalInSeconds;
  49. }
  50. }
  51. }
  52. #endif