TimeComponent.cs 1.2 KB

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