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.

118 lines
5.2KB

  1. #if UNITY_ECS
  2. using Svelto.Common;
  3. using Svelto.ECS.Schedulers;
  4. using Unity.Entities;
  5. using Unity.Jobs;
  6. namespace Svelto.ECS.Extensions.Unity
  7. {
  8. /// <summary>
  9. /// This is a high level class to abstract the complexity of creating a Svelto ECS application that interacts
  10. /// with UECS. However this is designed to make it work almost out of the box, but it should be eventually
  11. /// substituted by project customized code.
  12. /// This is a JobifiedEngine and as such it expect to be ticked. Normally it must be executed in a
  13. /// SortedEnginesGroup as step that happens after the Svelto jobified engines run.
  14. ///
  15. /// The flow should be:
  16. /// Svelto (GameLogic) Engines Run first
  17. /// Then this Engine runs, which causes:
  18. /// Jobs to be completed (it's a sync point)
  19. /// Synchronizations engines to be executed (Svelto to UECS)
  20. /// Submission of Entities to be executed
  21. /// Svelto Add/Remove callbacks to be called
  22. /// ISubmissionEngines to be executed
  23. /// UECS engines to executed
  24. /// Synchronizations engines to be executed (UECS To Svelto)
  25. /// </summary>
  26. [Sequenced(nameof(JobifiedSveltoEngines.SveltoOverUECS))]
  27. public class SveltoOverUECSEnginesGroup: IJobifiedEngine
  28. {
  29. public SveltoOverUECSEnginesGroup(EnginesRoot enginesRoot)
  30. {
  31. DBC.ECS.Check.Require(enginesRoot.scheduler is SimpleEntitiesSubmissionScheduler, "The Engines root must use a EntitiesSubmissionScheduler scheduler implementation");
  32. CreateUnityECSWorldForSvelto(enginesRoot.scheduler as SimpleEntitiesSubmissionScheduler, enginesRoot);
  33. }
  34. public World world { get; private set; }
  35. void CreateUnityECSWorldForSvelto(SimpleEntitiesSubmissionScheduler scheduler, EnginesRoot enginesRoot)
  36. {
  37. world = new World("Svelto<>UECS world");
  38. var systems = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default);
  39. DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(world, systems);
  40. World.DefaultGameObjectInjectionWorld = world;
  41. //This is the UECS group that takes care of all the UECS systems that creates entities
  42. //it also submits Svelto entities
  43. _sveltoUecsEntitiesSubmissionGroup = new SveltoUECSEntitiesSubmissionGroup(scheduler);
  44. //This is the group that handles the UECS sync systems that copy the svelto entities values to UECS entities
  45. enginesRoot.AddEngine(_sveltoUecsEntitiesSubmissionGroup);
  46. world.AddSystem(_sveltoUecsEntitiesSubmissionGroup);
  47. _syncSveltoToUecsGroup = new SyncSveltoToUECSGroup();
  48. enginesRoot.AddEngine(_syncSveltoToUecsGroup);
  49. _syncUecsToSveltoGroup = new SyncUECSToSveltoGroup();
  50. enginesRoot.AddEngine(_syncUecsToSveltoGroup);
  51. //This is the group that handles the UECS sync systems that copy the UECS entities values to svelto entities
  52. //enginesRoot.AddEngine(new SveltoUECSEntitiesSubmissionGroup(scheduler, world));
  53. enginesRoot.AddEngine(this);
  54. _enginesRoot = enginesRoot;
  55. }
  56. public JobHandle Execute(JobHandle inputDeps)
  57. {
  58. //this is a sync point, there won't be pending jobs after this
  59. _sveltoUecsEntitiesSubmissionGroup.SubmitEntities(inputDeps);
  60. //Mixed explicit job dependency and internal automatic ECS dependency system
  61. //Write in to UECS entities so the UECS dependencies react on the components touched
  62. var handle = _syncSveltoToUecsGroup.Execute(default);
  63. //As long as pure UECS systems do not use external containers (like native arrays and so) the Unity
  64. //automatic dependencies system will guarantee that there won't be race conditions
  65. world.Update();
  66. //this svelto group of UECS SystemBase systems
  67. return _syncUecsToSveltoGroup.Execute(handle);
  68. }
  69. public void AddUECSSubmissionEngine(SubmissionEngine submissionEngine)
  70. {
  71. _sveltoUecsEntitiesSubmissionGroup.Add(submissionEngine);
  72. _enginesRoot.AddEngine(submissionEngine);
  73. }
  74. public void AddSveltoToUECSEngine(SyncSveltoToUECSEngine engine)
  75. {
  76. //it's a Svelto Engine/UECS SystemBase so it must be added in the UECS world AND svelto enginesRoot
  77. world.AddSystem(engine);
  78. _enginesRoot.AddEngine(engine);
  79. _syncSveltoToUecsGroup.Add(engine);
  80. }
  81. public void AddUECSToSveltoEngine(SyncUECSToSveltoEngine engine)
  82. {
  83. //it's a Svelto Engine/UECS SystemBase so it must be added in the UECS world AND svelto enginesRoot
  84. world.AddSystem(engine);
  85. _enginesRoot.AddEngine(engine);
  86. _syncUecsToSveltoGroup.Add(engine);
  87. }
  88. public void Dispose()
  89. {
  90. world.Dispose();
  91. }
  92. public string name => nameof(SveltoOverUECSEnginesGroup);
  93. SveltoUECSEntitiesSubmissionGroup _sveltoUecsEntitiesSubmissionGroup;
  94. SyncSveltoToUECSGroup _syncSveltoToUecsGroup;
  95. SyncUECSToSveltoGroup _syncUecsToSveltoGroup;
  96. EnginesRoot _enginesRoot;
  97. }
  98. }
  99. #endif