For prefabs in a scene we can add the ConvertToEntity and PhysicsShapeAuthoring components to convert a static mesh with collider to an entity with a physicscollider. However, if I’m converting a prefab in code then instantiating entities at runtime, how do I convert the collider to a physicscollider?
Here is what I’m currently doing (without Unity.Physics) - this all works fine.
Entity prefabEntity = GameObjectConversionUtility.ConvertGameObjectHierarchy(prefabs[i].gameObject, conversionSettings);
..
EntityArchetype entityArcheType = world.EntityManager.CreateArchetype(archetypeComponentTypeArray);
..
world.GetOrCreateSystem<MySystem>();
// create x entities from prefab in a loop
..
Entity entity = entityManager.Instantiate(prefabEntity);
// Add components
..
// SetComponentData
..
I’m aware of PhysicsShapeAuthoring component but that seems to require a ConvertToEntity component which doesn’t seem to fit my scenario. I “could” create a new PhysicsCollider and add it as a component but what I really want is to convert the existing collider to a PhysicsCollider.
This should all work as is. You don’t need PhysicsShapeAuthoring components on your GameObjects. If you had a GameObject with a Collider and a RigidBody component, adding the ConvertToEntity component in a scene would still result in a Entity with a PhysicsCollider etc.
This is all handled by the BaseLegacyColliderConversionSystem which converts the GameObject based Colliders to PhysicsColliders and should get called as part of the call to ConvertGameObjectHierarchy.
Are you seeing converted GOs without PhysicsColliders being added?
Without Unity.Physics installed I can say instantiate 5000 prefabs using ConvertGameObjectHierarchy etc. When I install Unity.Physics 0.2.4 I get 1 visible entity and the rest don’t render. They do appear in the Entity Debugger. On closer observation, they do have a PhysicsCollider component however the Entity Debugger throws hundreds of errors as I click on the entities.
I have resolved the errors messages with the following:
// In my monobehaviour
private BlobAssetStore blobAssetStore;
private void ConvertPrefabs()
{
..
blobAssetStore = new BlobAssetStore();
GameObjectConversionSettings conversionSettings = GameObjectConversionSettings.FromWorld(world, blobAssetStore);
..
// more here..
}
private void OnDestroy()
{
// Dispose of the BlobAssetStore, else we're get a message:
// A Native Collection has not been disposed, resulting in a memory leak.
if (blobAssetStore != null) { blobAssetStore.Dispose(); }
}
I’m not sure if OnDestroy() is the correct place to dispose of the blobAssetStore but seems to work.
The only way I could get the prefabs to render with Unity.Physics 0.2.5 installed was to add another collider to the parent gameobject of the prefabs.
How can I get this to work with colliders in the children but not the parent? Without Unity.Physics installed it works fine (howbeit without PhysicsCollider components).
Update – Looks like NetCode is waiting for the Entities patch, as related in this thread
I’m not using ConvertGameObjectHierarchy but I’m seeing this same issue – for me I’ve actually removed both the Unity Physics collider from the prefab in (it’s converted automatically using the Ghost generation code from the multiplayer package). I also had an old school collider on a child plane GO that was being converted in the scene by a parent ConvertToClientServerEntity component – however I removed this too and am still getting the “ArgumentNullException: A valid BlobAssetStore must be passed to construct a BlobAssetComputationContext” – even upon restarting my editor. Seems there’s just something wrong here.