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.

UnityEntitiesSubmissionScheduler.cs 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #if UNITY_5 || UNITY_5_3_OR_NEWER
  2. using System;
  3. using Object = UnityEngine.Object;
  4. using System.Collections;
  5. using UnityEngine;
  6. namespace Svelto.ECS.Schedulers.Unity
  7. {
  8. //The EntitySubmissionScheduler has been introduced to make the entity components submission logic platform independent
  9. //You can customize the scheduler if you wish
  10. public class UnityEntitiesSubmissionScheduler : EntitiesSubmissionScheduler
  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();
  28. }
  29. }
  30. readonly WaitForEndOfFrame _wait = new WaitForEndOfFrame();
  31. readonly IEnumerator _coroutine;
  32. public System.Action onTick;
  33. }
  34. public UnityEntitiesSubmissionScheduler(string name)
  35. {
  36. _scheduler = new GameObject(name).AddComponent<Scheduler>();
  37. GameObject.DontDestroyOnLoad(_scheduler.gameObject);
  38. _scheduler.onTick = SubmitEntities;
  39. }
  40. public override void Dispose()
  41. {
  42. if (_scheduler != null && _scheduler.gameObject != null)
  43. {
  44. Object.Destroy(_scheduler.gameObject);
  45. }
  46. }
  47. public override bool paused { get; set; }
  48. void SubmitEntities()
  49. {
  50. if (paused == false)
  51. {
  52. var enumerator = _onTick.Invoke(UInt32.MaxValue);
  53. while (enumerator.MoveNext()) ;
  54. }
  55. }
  56. protected internal override EnginesRoot.EntitiesSubmitter onTick
  57. {
  58. set => _onTick = value;
  59. }
  60. readonly Scheduler _scheduler;
  61. EnginesRoot.EntitiesSubmitter _onTick;
  62. }
  63. }
  64. #endif