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.

72 lines
1.9KB

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