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.

100 lines
4.3KB

  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. [Sequenced(nameof(JobifiedSveltoEngines.SveltoOverUECS))]
  9. public class SveltoOverUECSEnginesGroup: IJobifiedEngine
  10. {
  11. public SveltoOverUECSEnginesGroup(EnginesRoot enginesRoot)
  12. {
  13. DBC.ECS.Check.Require(enginesRoot.scheduler is ISimpleEntitiesSubmissionScheduler, "The Engines root must use a EntitiesSubmissionScheduler scheduler implementation");
  14. CreateUnityECSWorldForSvelto(enginesRoot.scheduler as ISimpleEntitiesSubmissionScheduler, enginesRoot);
  15. }
  16. public World world { get; private set; }
  17. void CreateUnityECSWorldForSvelto(ISimpleEntitiesSubmissionScheduler scheduler, EnginesRoot enginesRoot)
  18. {
  19. world = new World("Svelto<>UECS world");
  20. var systems = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default);
  21. DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(world, systems);
  22. World.DefaultGameObjectInjectionWorld = world;
  23. //This is the UECS group that takes care of all the UECS systems that creates entities
  24. //it also submits Svelto entities
  25. _sveltoUecsEntitiesSubmissionGroup = new SveltoUECSEntitiesSubmissionGroup(scheduler, world);
  26. enginesRoot.AddEngine(_sveltoUecsEntitiesSubmissionGroup);
  27. //This is the group that handles the UECS sync systems that copy the svelto entities values to UECS entities
  28. _syncSveltoToUecsGroup = new SyncSveltoToUECSGroup();
  29. enginesRoot.AddEngine(_syncSveltoToUecsGroup);
  30. _syncUecsToSveltoGroup = new SyncUECSToSveltoGroup();
  31. enginesRoot.AddEngine(_syncUecsToSveltoGroup);
  32. //This is the group that handles the UECS sync systems that copy the UECS entities values to svelto entities
  33. //enginesRoot.AddEngine(new SveltoUECSEntitiesSubmissionGroup(scheduler, world));
  34. enginesRoot.AddEngine(this);
  35. _enginesRoot = enginesRoot;
  36. }
  37. public JobHandle Execute(JobHandle inputDeps)
  38. {
  39. //this is a sync point, there won't be pending jobs after this
  40. _sveltoUecsEntitiesSubmissionGroup.Execute(inputDeps);
  41. //Mixed explicit job dependency and internal automatic ECS dependency system
  42. //Write in to UECS entities so the UECS dependencies react on the components touched
  43. var handle = _syncSveltoToUecsGroup.Execute(default);
  44. //As long as pure UECS systems do not use external containers (like native arrays and so) the Unity
  45. //automatic dependencies system will guarantee that there won't be race conditions
  46. world.Update();
  47. //this svelto group of UECS SystemBase systems
  48. return _syncUecsToSveltoGroup.Execute(handle);
  49. }
  50. public void AddUECSSubmissionEngine(IUECSSubmissionEngine spawnUnityEntityOnSveltoEntityEngine)
  51. {
  52. _sveltoUecsEntitiesSubmissionGroup.Add(spawnUnityEntityOnSveltoEntityEngine);
  53. _enginesRoot.AddEngine(spawnUnityEntityOnSveltoEntityEngine);
  54. }
  55. public void AddSveltoToUECSEngine(SyncSveltoToUECSEngine engine)
  56. {
  57. //it's a Svelto Engine/UECS SystemBase so it must be added in the UECS world AND svelto enginesRoot
  58. world.AddSystem(engine);
  59. _enginesRoot.AddEngine(engine);
  60. _syncSveltoToUecsGroup.Add(engine);
  61. }
  62. public void AddUECSToSveltoEngine(SyncUECSToSveltoEngine engine)
  63. {
  64. //it's a Svelto Engine/UECS SystemBase so it must be added in the UECS world AND svelto enginesRoot
  65. world.AddSystem(engine);
  66. _enginesRoot.AddEngine(engine);
  67. _syncUecsToSveltoGroup.Add(engine);
  68. }
  69. public void Dispose()
  70. {
  71. world.Dispose();
  72. }
  73. public string name => nameof(SveltoOverUECSEnginesGroup);
  74. SveltoUECSEntitiesSubmissionGroup _sveltoUecsEntitiesSubmissionGroup;
  75. SyncSveltoToUECSGroup _syncSveltoToUecsGroup;
  76. SyncUECSToSveltoGroup _syncUecsToSveltoGroup;
  77. EnginesRoot _enginesRoot;
  78. }
  79. }
  80. #endif