Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Instantiating converted entities with child transforms

Discussion in 'Data Oriented Technology Stack' started by Neodamus, Aug 22, 2019.

  1. Neodamus

    Neodamus

    Joined:
    Jan 12, 2018
    Posts:
    11
    I'm converting a prefab into an entity using ConvertToEntity with mode "Convert and Destroy". This creates 5 different entities at runtime: 1 with a Child component (the parent): https://cl.ly/3ddb4668dbe2, and 4 with a Parent component (the children). This all corresponds with how the prefab is designed and seems fine.

    However, when I try to do EntityManager.Instantiate on the parent entity to create a copy, none of the Child data is transferred.

    The Child component is of type ISystemStateBufferElementData, so it doesn't seem possible to set this data using SetComponentData or SetSharedComponentData. I am able to get this data by doing EntityManager.GetBuffer<Child>, but I don't see any way of setting this buffer data to another entity.

    Is there a known/preferred way of instantiating prefabs converted to entities at runtime while also maintaining information about the transform hierarchy?
     
  2. Neodamus

    Neodamus

    Joined:
    Jan 12, 2018
    Posts:
    11
    I came up with a solution for this, but if anyone has a better solution, I'd love to hear it.

    Code (CSharp):
    1.                 Unity.Entities.EntityManager mgr = Unity.Entities.World.Active.EntityManager;
    2.                 Unity.Entities.Entity originalEntity = OriginalEntities[0];
    3.                 Unity.Entities.Entity parentEntityCopy = mgr.Instantiate(OriginalEntities[0]);
    4.  
    5.                 DynamicBuffer<Child> childs = mgr.GetBuffer<Child>(originalEntity);
    6.                 foreach (var child in childs) {
    7.                     Unity.Entities.Entity childEntityCopy = mgr.Instantiate(child.Value);
    8.                     Parent parentData = new Parent() { Value = parentEntityCopy };
    9.                     mgr.SetComponentData(childEntityCopy, parentData);
    10.                 }
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    1,568
    Do you actually have 4 child GameObjects on your prefab? Your prefab Entity should have a LinkedEntityGroup with all the children.
     
    Neodamus likes this.
  4. Neodamus

    Neodamus

    Joined:
    Jan 12, 2018
    Posts:
    11
    Thanks for that. I was only using the ConvertToEntity component, and not GameObjectConversionUtility.ConvertGameObjectHierarchy, as done in the ECS Demos. Only using the component does not create a LinkedEntityGroup.
     
unityunity