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.

SveltoOnDOTSHandleLifeTimeEngine.cs 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #if UNITY_ECS
  2. using Unity.Collections;
  3. using Unity.Entities;
  4. namespace Svelto.ECS.SveltoOnDOTS
  5. {
  6. /// <summary>
  7. /// Automatic Svelto Group -> DOTS archetype synchronization when necessary
  8. /// </summary>
  9. /// <typeparam name="DOTSEntityComponent"></typeparam>
  10. class SveltoOnDOTSHandleLifeTimeEngine<DOTSEntityComponent>: ISveltoOnDOTSStructuralEngine, IReactOnRemoveEx<DOTSEntityComponent>,
  11. IReactOnSwapEx<DOTSEntityComponent> where DOTSEntityComponent : unmanaged, IEntityComponentForDOTS
  12. {
  13. public void Remove((uint start, uint end) rangeOfEntities, in EntityCollection<DOTSEntityComponent> entities, ExclusiveGroupStruct groupID)
  14. {
  15. var (buffer, _) = entities;
  16. var nativeArray = new NativeArray<Entity>((int)(rangeOfEntities.end - rangeOfEntities.start), Allocator.Temp);
  17. //todo this could be burstified or memcpied
  18. int counter = 0;
  19. for (uint i = rangeOfEntities.start; i < rangeOfEntities.end; i++)
  20. nativeArray[counter++] = buffer[i].dotsEntity;
  21. DOTSOperations.DestroyEntitiesBatched(nativeArray);
  22. }
  23. public void MovedTo((uint start, uint end) rangeOfEntities, in EntityCollection<DOTSEntityComponent> entities,
  24. ExclusiveGroupStruct _, ExclusiveGroupStruct toGroup)
  25. {
  26. var (buffer, _) = entities;
  27. var nativeArray = new NativeArray<Entity>((int)(rangeOfEntities.end - rangeOfEntities.start), Allocator.Temp);
  28. //todo this could be burstified or memcpied
  29. int counter = 0;
  30. for (uint i = rangeOfEntities.start; i < rangeOfEntities.end; i++)
  31. nativeArray[counter++] = buffer[i].dotsEntity;
  32. DOTSOperations.SetSharedComponentBatched(nativeArray, new DOTSSveltoGroupID(toGroup));
  33. }
  34. public void OnPostSubmission() { }
  35. public DOTSOperationsForSvelto DOTSOperations { get; set; }
  36. public string name => nameof(SveltoOnDOTSHandleLifeTimeEngine<DOTSEntityComponent>);
  37. }
  38. }
  39. #endif