Hybrid GameObject-Entity handling

Apologies if this has been posted in some capacity before - it has proven remarkably difficult to find an answer to this, Google seems to think I’m only asking about one or the other, over an hour of searching and has yielded very little in terms of up-to-date information with Entities and practically nothing on these hybrid approaches in >v1.0.

I have a project currently implemented in terms of GameObjects. Among these is a GameObject prefab that contains a number of further components, such as networking, mesh rendering, some little scripts, but crucially a MeshCollider. My goal is to move the MeshCollider and other physics processing parts over to DOTS to leverage Unity Physics.

So far, I have managed to set up physics authoring for my static objects (eg. the map) and dynamic objects (eg. my prefabs). This includes swapping the MeshCollider out for a PhysicsCollider, conditionally setting up PhysicsVelocity and PhysicsMass, etc.

The part I am stuck on is the runtime instantiation of these prefabs. As far as I can see, where I previously had a simple GameObject.Instantiate(prefab), I now need to additionally have something create an Entity and then somehow couple these two such that the GameObject copies the transform of the Entity (as the transform is required by other GameObject components). I have written a sync system to do this:

ForEach((ref LocalTransform localTransform, in GameObjectRef gameObjectRef) => //copy pos, rot

but can’t figure out how to actually add the GameObjectRef component to anything:

public struct GameObjectRef : IComponentData
{
    public GameObject GameObject;
}

//...
//meanwhile, in my spawner system
var gameObject = Object.Instantiate(prefab);
ecb.AddComponent(entity, new GameObjectRef
{
    GameObject =  gameObject
});

as GameObject is not a native type.

I feel like I might be going about this all wrong, if there is a generally accepted as sane way of managing these hybrid cases I’d love to know about it.

Regards

Try using UnityObjectRef<GameObject> as your field in your IComponentData`.

That seems to work. Thank you!
I also found that in removing the MeshCollider, thinking I was being smart by replacing it with a PhysicsCollider through an authoring script, causes more issues as Physics components are already added by things like RigidBody, HingeJoint, FixedJoint and the like… I reverted much of those physics authoring doodads and just left it using the built-in bakers, but now the spawned Entity immediately has all its physics components set to NaN by frame 2. I’ll be back with a new post if I can’t figure that one out :thinking: