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.

61 lines
2.4KB

  1. #if UNITY_ECS
  2. using Svelto.ECS.Schedulers;
  3. using Unity.Entities;
  4. using Unity.Jobs;
  5. namespace Svelto.ECS.Extensions.Unity
  6. {
  7. /// <summary>
  8. /// Group of UECS/Svelto SystemBase engines that creates UECS entities.
  9. /// Svelto entities are submitted
  10. /// Svelto Add and remove callback are called
  11. /// OnUpdate of the systems are called
  12. /// finally the UECS command buffer is flushed
  13. /// Note: I cannot use Unity ComponentSystemGroups nor I can rely on the SystemBase Dependency field to
  14. /// solve external dependencies. External dependencies are tracked, but only linked to the UECS components operations
  15. /// With Dependency I cannot guarantee that an external container is used before previous jobs working on it are completed
  16. /// </summary>
  17. public class SveltoUECSEntitiesSubmissionGroup : JobifiedEnginesGroup<IUECSSubmissionEngine>
  18. {
  19. public SveltoUECSEntitiesSubmissionGroup
  20. (ISimpleEntitiesSubmissionScheduler submissionScheduler, World UECSWorld)
  21. {
  22. _submissionScheduler = submissionScheduler;
  23. _ECBSystem = UECSWorld.CreateSystem<SubmissionEntitiesCommandBufferSystem>();
  24. }
  25. public new void Execute(JobHandle jobHandle)
  26. {
  27. //Sync Point as we must be sure that jobs that create/swap/remove entities are done
  28. jobHandle.Complete();
  29. if (_submissionScheduler.paused)
  30. return;
  31. //prepare the entity command buffer to be used by the registered engines
  32. var entityCommandBuffer = _ECBSystem.CreateCommandBuffer();
  33. foreach (var system in _engines)
  34. {
  35. system.ECB = entityCommandBuffer;
  36. system.EM = _ECBSystem.EntityManager;
  37. }
  38. //Submit Svelto Entities, calls Add/Remove/MoveTo that can be used by the IUECSSubmissionEngines
  39. _submissionScheduler.SubmitEntities();
  40. //execute submission engines and complete jobs
  41. base.Execute(default).Complete();
  42. //flush command buffer
  43. _ECBSystem.Update();
  44. }
  45. readonly ISimpleEntitiesSubmissionScheduler _submissionScheduler;
  46. readonly SubmissionEntitiesCommandBufferSystem _ECBSystem;
  47. [DisableAutoCreation]
  48. class SubmissionEntitiesCommandBufferSystem : EntityCommandBufferSystem { }
  49. }
  50. }
  51. #endif