Mirror of Svelto.ECS because we're a fan of it
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.

66 lines
1.7KB

  1. #if UNITY_5 || UNITY_5_3_OR_NEWER
  2. using Object = UnityEngine.Object;
  3. using System;
  4. using System.Collections;
  5. using UnityEngine;
  6. namespace Svelto.ECS.Schedulers.Unity
  7. {
  8. //The EntitySubmissionScheduler has been introduced to make the entity views submission logic platform independent
  9. //You can customize the scheduler if you wish
  10. public class UnityEntitySubmissionScheduler : IEntitySubmissionScheduler
  11. {
  12. class Scheduler : MonoBehaviour
  13. {
  14. public Scheduler()
  15. {
  16. _coroutine = Coroutine();
  17. }
  18. void Update()
  19. {
  20. _coroutine.MoveNext();
  21. }
  22. IEnumerator Coroutine()
  23. {
  24. while (true)
  25. {
  26. yield return _wait;
  27. onTick.Invoke();
  28. }
  29. }
  30. readonly WaitForEndOfFrame _wait = new WaitForEndOfFrame();
  31. readonly IEnumerator _coroutine;
  32. public EnginesRoot.EntitiesSubmitter onTick;
  33. }
  34. public UnityEntitySubmissionScheduler(string name = "ECSScheduler") { _name = name; }
  35. public void Dispose()
  36. {
  37. Object.Destroy(_scheduler.gameObject);
  38. }
  39. public EnginesRoot.EntitiesSubmitter onTick
  40. {
  41. set
  42. {
  43. if (_scheduler == null)
  44. {
  45. _scheduler = new GameObject(_name).AddComponent<Scheduler>();
  46. GameObject.DontDestroyOnLoad(_scheduler.gameObject);
  47. }
  48. _scheduler.onTick = value;
  49. }
  50. }
  51. Scheduler _scheduler;
  52. readonly string _name;
  53. }
  54. }
  55. #endif