Updater.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using UnityEngine;
  3. namespace VEngine
  4. {
  5. public sealed class Updater : MonoBehaviour
  6. {
  7. public static Action updated;
  8. public float maxUpdateTimeSlice = 0.01f;
  9. public float time { get; private set; }
  10. public bool busy => Time.realtimeSinceStartup - time >= maxUpdateTimeSlice;
  11. public static Updater Instance { get; private set; }
  12. private void Awake()
  13. {
  14. Instance = this;
  15. }
  16. private void Update()
  17. {
  18. time = Time.realtimeSinceStartup;
  19. Loadable.UpdateAll();
  20. Operation.UpdateAll();
  21. Download.UpdateAll();
  22. if (updated != null) updated.Invoke();
  23. }
  24. private void OnDestroy()
  25. {
  26. Download.ClearAllDownloads();
  27. }
  28. [RuntimeInitializeOnLoadMethod]
  29. private static void InitializeOnLoad()
  30. {
  31. var updater = FindObjectOfType<Updater>();
  32. if (updater == null)
  33. {
  34. updater = new GameObject("Updater").AddComponent<Updater>();
  35. DontDestroyOnLoad(updater);
  36. }
  37. }
  38. }
  39. }