ECS Runtime-created collider is unique by default - possible BUG?

I have a DynamicBuffer of positions on which I want to create sphere colliders. I am using following system to create colliders in runtime:

partial struct CreateCollidersSystem : ISystem
{
    private BlobAssetReference<Unity.Physics.Collider> CreatedColliderBlob;

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        // Do it only once
        state.Enabled = false;

        // Define sphere geometry
        SphereGeometry sphereGeometry = new SphereGeometry
        {
            Center = float3.zero,
            Radius = 1f
        };

        // Create collider blob
        CreatedColliderBlob = Unity.Physics.SphereCollider.Create(sphereGeometry, new CollisionFilter
        {
            BelongsTo = (uint)CollisionLayers.Ground,
            CollidesWith = (uint)CollisionLayers.Players
        });

        EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.Temp);
        DynamicBuffer<WaypointElement> waypointsBuffer = SystemAPI.GetSingletonBuffer<WaypointElement>();
        
        // Add collider to each point in buffer
        foreach (WaypointElement waypoint in waypointsBuffer)
        {
            ecb.AddComponent(waypoint.Entity, new PhysicsCollider { Value = CreatedColliderBlob });
            ecb.AddSharedComponent(waypoint.Entity, new PhysicsWorldIndex { Value = 0 });
        }

        ecb.Playback(state.EntityManager);
        ecb.Dispose();
    }

    [BurstCompile]
    public void OnDestroy(ref SystemState state)
    {
        if (CreatedColliderBlob.IsCreated)
            CreatedColliderBlob.Dispose();
    }
}

Although colliders are created correctly, after inspection in editor I can see that each one is marked as unique:

Why it should be unique when I am using single blob to define all the colliders? I believe correct behaviour should be non-unique state by default.

Am I missing something here? Or is it a bug? I am using latest versions of the Physics and Entities (1.3.8).

It unfortunately looks like this is currently by design.

Collider.cs:1417

public uint ForceUniqueBlobID; 
// ID used to force a unique collider blob in entities.
// When a collider is manually created or cloned, the ID is set to ~ColliderConstants.k_SharedBlobID
// marking the collider as unique.
// When a collider is created during baking through the blob asset store, the ID is set to
// ColliderConstants.k_SharedBlobID in order to enable sharing of identical collider blobs among entities.
// The sharing is disabled by the baking systems through setting of this value to a unique value when the user
// requests unique collider blobs via the force unique authoring option.
1 Like

Ah, ok, thanks for response, that’s unfortunate indeed. Maybe someone from Unity ECS devs could respond what’s the reason behind it?

Hi, also encounter this behavior when as part of baking I create my own collider for my character controller.
This is causing me head heck because there is apparently no way to get a shared collider except through the built in authoring components.

This cause my collider to be unique and for prefabs to had a Collider Blob Cleanup which waste memory and reduce chunk capacity.

Is there any plan to allow us to create non unique colliders from script ?

Hi!

I can maybe clarify what is going on here, and ease your concerns.

In essence, if you run-time create collider blobs, they are unique by default because Unity Physics does not automatically take care of any sharing of the collider blobs between PhysicsCollider components (aka rigid bodies).
This doesn’t prevent you from implementing collider blob sharing yourself. So, if you want to yourself use the same run-time created collider blob in multiple PhysicsColliders though, you can do so without any issues. You just need to take care of manually managing their disposal correctly, which you have to do with run-time created blobs anyways.
For example, you could just create a NativeList of the run-time created blobs and dispose of them in the OnDestroy function in some ISystem or so.

I hope this clarifies this situation and makes it clear that there is no limitation here if you want to share run-time created collider blobs yourself for efficiency.