TimeComponent.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.Search;
  4. using UnityEngine;
  5. using Utility;
  6. namespace Mono.UI.Core
  7. {
  8. public class TimeComponent : Singleton<TimeComponent>
  9. {
  10. public Queue<CustomTimer> TimerPool = new Queue<CustomTimer>();
  11. public List<CustomTimer> CustomTimers = new List<CustomTimer>();
  12. public void Init()
  13. {
  14. }
  15. public void AddTimer(float time, Action action)
  16. {
  17. CustomTimer customTimer;
  18. if (TimerPool.Count > 0)
  19. {
  20. customTimer = TimerPool.Dequeue();
  21. }
  22. else
  23. {
  24. customTimer = new CustomTimer();
  25. }
  26. customTimer.CustomInit(time, action);
  27. CustomTimers.Add(customTimer);
  28. }
  29. public void Update()
  30. {
  31. for (var i = 0; i < CustomTimers.Count; i++)
  32. {
  33. CustomTimers[i].Update(Time.deltaTime);
  34. }
  35. }
  36. public void Rec(CustomTimer customTimer)
  37. {
  38. if (CustomTimers.Contains(customTimer))
  39. {
  40. CustomTimers.Remove(customTimer);
  41. TimerPool.Enqueue(customTimer);
  42. }
  43. }
  44. }
  45. }