Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instantiating converted entities with child transforms

Discussion in 'Entity Component System' started by Neodamus, Aug 22, 2019.

  1. Neodamus

    Neodamus

    Joined:
    Jan 12, 2018
    Posts:
    14
    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:
    14
    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:
    3,984
    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:
    14
    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.