Skip to content

Instantly share code, notes, and snippets.

@LukasKastern
Created March 3, 2021 19:41
Show Gist options
  • Save LukasKastern/0116c2884421710a87005ab3bae862bf to your computer and use it in GitHub Desktop.
Save LukasKastern/0116c2884421710a87005ab3bae862bf to your computer and use it in GitHub Desktop.
using Unity.Collections.LowLevel.Unsafe;
namespace Unity.Entities
{
[NativeContainer]
public unsafe struct EntityExistsFromEntity
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
readonly AtomicSafetyHandle m_Safety;
#endif
[NativeDisableUnsafePtrRestriction]
readonly EntityComponentStore* m_EntityComponentStore;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
internal EntityExistsFromEntity( EntityComponentStore* store, AtomicSafetyHandle safety )
{
m_EntityComponentStore = store;
m_Safety = safety;
}
#else
internal EntityExistsFromEntity( EntityComponentStore* store )
{
m_EntityComponentStore = store;
}
#endif
public bool Exists(Entity entity)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
#endif
return m_EntityComponentStore->Exists(entity);
}
}
public static class EntityExistsExtension
{
public static EntityExistsFromEntity GetEntityExistsFromEntity( this ComponentSystemBase system )
{
unsafe
{
var ecs = system.EntityManager.GetCheckedEntityDataAccess();
#if ENABLE_UNITY_COLLECTIONS_CHECKS
var safetyHandles = &ecs->DependencyManager->Safety;
return new EntityExistsFromEntity( ecs->EntityComponentStore, safetyHandles->GetSafetyHandleForEntityTypeHandle( ) );
#else
return new EntityExistsFromEntity(ecs->EntityComponentStore);
#endif
}
}
public static EntityExistsFromEntity GetEntityExistsFromEntity( this EntityManager entityManager )
{
unsafe
{
var ecs = entityManager.GetCheckedEntityDataAccess();
#if ENABLE_UNITY_COLLECTIONS_CHECKS
var safetyHandles = &ecs->DependencyManager->Safety;
return new EntityExistsFromEntity( ecs->EntityComponentStore, safetyHandles->GetSafetyHandleForEntityTypeHandle( ) );
#else
return new EntityExistsFromEntity(ecs->EntityComponentStore);
#endif
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment