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.

38 line
1.5KB

  1. #if UNITY_NATIVE //at the moment I am still considering NativeOperations useful only for Unity
  2. using System.Runtime.CompilerServices;
  3. using Svelto.DataStructures;
  4. namespace Svelto.ECS.Native
  5. {
  6. public readonly ref struct NativeEntityInitializer
  7. {
  8. readonly NativeBag _unsafeBuffer;
  9. readonly UnsafeArrayIndex _componentsToInitializeCounterRef;
  10. readonly EntityReference _reference;
  11. public NativeEntityInitializer(in NativeBag unsafeBuffer, UnsafeArrayIndex componentsToInitializeCounterRef, EntityReference reference)
  12. {
  13. _unsafeBuffer = unsafeBuffer;
  14. _componentsToInitializeCounterRef = componentsToInitializeCounterRef;
  15. _reference = reference;
  16. }
  17. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  18. public ref T Init<T>(in T component) where T : unmanaged, IEntityComponent
  19. {
  20. uint componentID = EntityComponentID<T>.ID.Data;
  21. _unsafeBuffer.AccessReserved<uint>(_componentsToInitializeCounterRef)++; //increase the number of components that have been initialised by the user
  22. //Since NativeEntityInitializer is a ref struct, it guarantees that I am enqueueing components of the
  23. //last entity built
  24. _unsafeBuffer.Enqueue(componentID); //to know what component it's being stored
  25. _unsafeBuffer.ReserveEnqueue<T>(out var index) = component;
  26. return ref _unsafeBuffer.AccessReserved<T>(index);
  27. }
  28. public EntityReference reference => _reference;
  29. }
  30. }
  31. #endif