Search Unity

Intended way of instantiate a entities in parent child relationship with entitycommandbuffer?

Discussion in 'Entity Component System' started by yc960, Sep 6, 2018.

  1. yc960

    yc960

    Joined:
    Apr 30, 2015
    Posts:
    228
    let's say I have entities A-B-C in a parent child hierarchy. What is the best way of repeatedly instantiate that group of entities with hierarchy maintained?
     
  2. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Also wondering this. I'm doing
    commandBuffer.Instantiate(index, entity);
    on a Parent entity with the expectation that it would instantiate copies of it's child entities as well. Instead it only instantiates the parent entity - I guess it ignores Dynamic Buffers?
     
  3. digitaliliad

    digitaliliad

    Joined:
    Jul 1, 2018
    Posts:
    64
    This should be achievable with a LinkedEntityGroup:
    https://gametorrahod.com/gameobject-conversion-ecosystem-code-tour/
     
    merpheus and Sarkahn like this.
  4. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    It seems some things reference in that article are out of date - "AddReferencedPrefabAsGroup" is not a thing. But it did set me on the right track.

    When you convert a gameobject using "GameObjectConversionUtility.ConvertGameObjectHierarchy" it will convert the hierarchy as expected AND put everything into a LinkedEntityGroup. At that point you can call instantiate on the converted entity and it will work as expected.

    However if you use the "IConvertGameObjectToEntity" workflow, it will NOT put everything into a LinkedEntityGroup. It creates the child buffer and parent components as expected but unlike the ConversionUtility method it doesn't add a LinkedEntityGroup to the parent, meaning if you try to instantiate an entity created via IConvertGameObjectToEntity you will only instantiate the parent.

    I'm not sure if this is a bug or if there's a good reason for the different behaviour between the two methods.
     
    florianhanke likes this.
  5. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Messing with this more in the newest version (p.32) and we now have:

    GameObjectConversionSystem.AddLinkedEntityGroup( GameObect )

    If you call this on your parent object which has the ConvertToEntity script it appears to add all children in that parent in a LinkedEntityGroup on the parent. So along with whatever other ComponentConversion you do on your parent you can add:
    Code (CSharp):
    1.  
    2. public class LinkedGroupConversion : MonoBehaviour, IConvertGameObjectToEntity
    3. {
    4.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    5.     {
    6.         conversionSystem.AddLinkedEntityGroup(gameObject);
    7.     }
    8. }
    9.  
    Then when you instantiate that parent entity from the ECS side it will work the same way as it does on the Gameobject side.

    devenv_8gNtxoUgBl.png


    devenv_9nNJK9DIL4.png
     
    NotaNaN, eterlan and digitaliliad like this.
  6. jintaenate

    jintaenate

    Joined:
    Sep 12, 2018
    Posts:
    17
    Omg this code is amazing for me thanks
     
  7. whiitehead

    whiitehead

    Joined:
    Jan 24, 2020
    Posts:
    6
    This saved my butt. Thanks

    I'm pretty sure this has been changed to
    DeclareLinkedEntityGroup