A GameObject can be moved around by the user during run time
I would like to keep an IComponentData in sync with that GameObject’s Position and Rotation (Transform)
I found that ConvertToEntity does not keep the Entity data in sync with the GameObject that it converted from
I was thinking of putting a reference to the parent GameObject in a component, and then finding the transform of the referred GameObject but I am not sure if looking that up is computationally expensive
I could have the GameObject run a script that detects a change of location, and if it is changed, to pull all IComponentDatas that are tracking GameObjects and update the IComponentData tracking its Transform
What is the best way to keep an IComponent data in sync with a move-able GameObject?
I have poor knowledge of hybrid workflows, so maybe there’s already pre-made stuff that can help you with this. But if not, I’d suggest:
Create an Entity with these components: Translation, Rotation, GameObjectLink
GameObjectLink would be a custom component you’ve made, which holds a ref to a gameobject. It would be a “class-based” IComponentData, meaning it can hold managed types and you’d define it like this:
public class GameObjectLink : IComponentData
{
public GameObject GameObject;
}
Have a GameObjectLinkSystem that, in its update, will do a job like this:
There’s actually quite a few ways to do this. The real question is how many things do you need to keep in sync? The solutions proposed so far are simple which makes them good for a small amount of transforms. But for larger counts, there are techniques to fully utilize jobs and Burst.
Once you start getting closer to 100 or more, it may be worth using jobs and Burst. The key is to maintain a mapping of array indices to entities. Each index corresponds to a packed array of Transforms, specifically a TransformAccessArray. Then you can use IJobParallelForTransform and your mapping to do your synchronization.
I use this for cameras to avoid completing transform jobs and keep my job chain going. I also use this in an animation experiment I am working on where I have several invisible IK driven characters and I have a few thousand entities blending between two of the GameObject poses each frame.